By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

Is cast operator unary or binary? How many operands?

P: n/a
How may operators and operands does (typename) expression has?

I'd say one operator, the cast operator, and two operands: typename
and expression.

But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?

Thanks.

José María.
Jun 27 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Apr 29, 3:45 pm, JoseMariaSola <JoseMariaS...@gmail.comwrote:
How may operators and operands does (typename) expression has?

I'd say one operator, the cast operator, and two operands: typename
and expression.

But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?
(typename)(expression) has one operator `(typename)' and one operand
`(expression)'.
The reason the type is enclosed in parentheses (as a design decision)
is probably to avoid ambiguity, consider this:

int i = 1; /* define and initialize i to 1 */
{
int i; /* cast i to int, a statement with no effect, or define i in
block scope? */
}
Jun 27 '08 #2

P: n/a
I'd say one operator, the cast operator, and two operands: typename
and expression.
But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?

(typename)(expression) has one operator `(typename)' and one operand
`(expression)'.
The reason the type is enclosed in parentheses (as a design decision)
is probably to avoid ambiguity, consider this:

int i = 1; /* define and initialize i to 1 */
{
int i; /* cast i to int, a statement with no effect, or define i in
block scope? */

}
Thanks, Vipps.

According to your answeer, the operator '(typename)' is very
particular, because it not a single token but three AND the middle
token is anything an identifier may be.

JM.
Jun 27 '08 #3

P: n/a
On Apr 29, 1:47 pm, JoseMariaSola <JoseMariaS...@gmail.comwrote:
I'd say one operator, the cast operator, and two operands: typename
and expression.
But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?
(typename)(expression) has one operator `(typename)' and one operand
`(expression)'.
The reason the type is enclosed in parentheses (as a design decision)
is probably to avoid ambiguity, consider this:
int i = 1; /* define and initialize i to 1 */
{
int i; /* cast i to int, a statement with no effect, or define i in
block scope? */
}

Thanks, Vipps.

According to your answeer, the operator '(typename)' is very
particular, because it not a single token but three AND the middle
token is anything an identifier may be.

JM.
The last line of my last post shoudl be:
... the middle token is anything an identifier may be and more.

Here is part of the grammar:

unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

unary-operator: one of
& * + - ˜ !

cast-expression:
unary-expression
( type-name ) cast-expression
Why sizeof, (type-name), ++ and -- aren't unary-operators?

Thanks.
Jun 27 '08 #4

P: n/a
In article <5c**********************************@25g2000hsx.g ooglegroups.com>,
JoseMariaSola <Jo***********@gmail.comwrote:
>Here is part of the grammar:

unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

unary-operator: one of
& * + - ˜ !

cast-expression:
unary-expression
( type-name ) cast-expression
Why sizeof, (type-name), ++ and -- aren't unary-operators?
You could include sizeof in unary-operator, but you'd still need the
special case of a type-name operand requiring parentheses, and it
seems clearer to keep them together in the list. Similarly ++ and --
would still have to appear in postfix-expression. Both of these are
just matters of taste really. I don't see how you could do
(type-name), because a unary-operator is something that precedes its
operand, and the parentheses of a cast have to go around the operand.

-- Richard

--
:wq
Jun 27 '08 #5

P: n/a
On Apr 29, 2:18 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <5c21f815-e1e6-468e-8630-cc72dad60...@25g2000hsx.googlegroups.com>,

JoseMariaSola <JoseMariaS...@gmail.comwrote:
Here is part of the grammar:
unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )
unary-operator: one of
& * + - ˜ !
cast-expression:
unary-expression
( type-name ) cast-expression
Why sizeof, (type-name), ++ and -- aren't unary-operators?

You could include sizeof in unary-operator, but you'd still need the
special case of a type-name operand requiring parentheses, and it
seems clearer to keep them together in the list. Similarly ++ and --
would still have to appear in postfix-expression. Both of these are
just matters of taste really.
I don't see how you could do
(type-name), because a unary-operator is something that precedes its
operand, and the parentheses of a cast have to go around the operand.
But in the case of cast the (only) operand is the expression the left,
not the type-name.

Talking with you both I notice that every operator requires
expressions as operands, and type-name is not an expression. Am I
right?

JM.
Jun 27 '08 #6

P: n/a
JoseMariaSola <Jo***********@gmail.comwrites:
[...]
Talking with you both I notice that every operator requires
expressions as operands, and type-name is not an expression. Am I
right?
Ideally, yes, but C is not an ideal language (not that I'm arguing it
needs to be).

"sizeof" is a unary operator; its operand can be either an expression
(specifically a unary-expression) or a parenthesized type name.

Or, if you prefer, "sizeof ( type-name )" is a special form of
expression. You don't *have* to think of the parenthesized type name
as an operand. But the standard puts it in the same section with the
real unary operators, probably because the other form of "sizeof" is
in that section. It would have been more logical, but less clear, to
separate them.

"." and "->" could be thought of as binary operators whose right
operand is an identifier, but the standard treats them as postfix
operators. Given:
struct foo { int x; int y; };
you can think of ".x" and ".y" as two distinct postfix operators, both
applicable to operands of type "struct foo".

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7

P: n/a
In article <13**********************************@x35g2000hsb. googlegroups.com>,
JoseMariaSola <Jo***********@gmail.comwrote:
>But in the case of cast the (only) operand is the expression the left,
not the type-name.
I see, you want to consider, say, (int) as an operator with a single
operand, rather than (int)x containing an operator with two operands,
"int" and "x".

This would imply an infinite number of operators, which is not
out of the question but doesn't seem to offer any advantage.
>Talking with you both I notice that every operator requires
expressions as operands, and type-name is not an expression. Am I
right?
The two traditional uses of "( type-name )" - in casts and sizeof -
and the new use in C99 - in compound literals - are indeed
exceptional. I previously suggested that we could factor out

type-expression:
( type-name )

which would make sizeof in particular more regular:

unary-expression:
...
sizeof unary-expression
sizeof type-expression

-- Richard
--
:wq
Jun 27 '08 #8

P: n/a
On Apr 29, 5:31*pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <13115f63-fd53-4436-879d-4dab6f0fc...@x35g2000hsb.googlegroups.com>,

JoseMariaSola *<JoseMariaS...@gmail.comwrote:
But in the case of cast the (only) operand is the expression the left,
not the type-name.

I see, you want to consider, say, (int) as an operator with a single
operand, rather than (int)x containing an operator with two operands,
"int" and "x".

This would imply an infinite number of operators, which is not
out of the question but doesn't seem to offer any advantage.
Talking with you both I notice that every operator requires
expressions as operands, and type-name is not an expression. Am I
right?

The two traditional uses of "( type-name )" - in casts and sizeof -
and the new use in C99 - in compound literals - are indeed
exceptional. *I previously suggested that we could factor out

* type-expression:
* * *( type-name )

which would make sizeof in particular more regular:

* unary-expression:
* * *...
* * *sizeof unary-expression
* * *sizeof type-expression

-- Richard
--
:wq
Thanks, Richard.

So both following expressions have one operator and one operand?
sizeof(int)
(int)x

typename is considered an operand in the firs expressin but not in the
second one?

JM.
Jun 27 '08 #9

P: n/a
In article <f6**********************************@x35g2000hsb. googlegroups.com>,
JoseMariaSola <Jo***********@gmail.comwrote:
>But in the case of cast the (only) operand is the expression the left,
not the type-name.
[...]
>So both following expressions have one operator and one operand?
sizeof(int)
(int)x
*I* dont' consider it that way. I was just exploring the consequences
of your suggestion. I would say that they have one and two operands
respectively.

-- Richard
--
:wq
Jun 27 '08 #10

P: n/a
In article <f6**********************************@x35g2000hsb. googlegroups.com>
JoseMariaSola <Jo***********@gmail.comwrote:
>So both following expressions have one operator and one operand?
sizeof(int)
(int)x
Yes.
>typename is considered an operand in the firs expressin but not in the
second one?
Yes. (In the second one, the type name is part of the operator.)

This is just a matter of terminology, in this case, as applied to
parsing. Since the terminology is made up by humans, it need not
be entirely consistent, or even make all that much sense, as long
as it lets one human communicate with another. :-)

The way to view this, to make sense out of it, is that the cast
operator is inherently made up of the token sequence '(',
type-name-keyword [%], ')'. Having consumed the type-name by this
point, the only "argument" left to call an "operand" is the value
being cast. The sizeof operator is inherently made up of the
keyword token 'sizeof', and if its operand is a type-name, the
type-name is syntactically required to have outer-level parentheses.
Since the parentheses are (again) consumed by the syntax, the
"argument" that is left, that can be called an "operand", is the
type-name (without, in this case, the parentheses, although it
makes no real difference anyway).

[% In C as originally defined by Dennis Ritchie, "typedef" did not
even exist. So all type names were keywords, which made parsing
C easy. All declarations were instantly obvious, as they always
started with a keyword, and since the total set of keywords was
small, everyone knew all of them at a glance. When typedef was
later added, some time in the late 1970s, the language became much
more complicated syntactically, since typedef identifiers are only
distinguishable from other identifiers by context. For instance,
the C89 -- but not C99 -- code fragment:

void f(x);

can mean either:

- declare f as function taking one argument of type int, and
returning void; or

- declare f as function taking one argument of type x, and
returning void.

The former makes use of C89's "implicit int"; the latter occurs if
and only if "x" is a typedef-name. C99 removes this particular
ambiguity by outlawing "implicit int", but other difficult parsing
situations remain. From the parser's point of view, typedef-ed
identifiers are "just like" keywords, except when they are not.
If one attempts to use a conventional scanner and parser generator
like (f)lex and (b)yacc, one must invent clever feedback and/or
feed-forward mechanisms by which the parser can tell the lexer
whether to claim a potential typedef name is a "type name" or an
"identifier name". (Or one could use a more powerful parsing
technique, but that means abandoning yacc.)

Because it is impossible to tell at a glance whether some identifier
is actually a typedef-name, C programmers often find themselves
using some sort of self-imposed (or company coding standard imposed)
typographic convention to "mark" all typedefs. For instance, some
people use _t suffixes, so that it is clear that greeble_t is a
type-name. Others use an initial capital letter, or all-uppercase.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #11

P: n/a
JoseMariaSola wrote:
How may operators and operands does (typename) expression has?

I'd say one operator, the cast operator, and two operands: typename
and expression.

But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?

Thanks.

José María.
It has one argument: the expression, and a *result*
of type "typename"

When I write

(double)i;

I am conceptually calling

double TransformIntegerToDouble(int);

a function that takes ONE integer argument and outputs
a double number. Obviously there is no such a function
since the compiler inlines this cast when compiling, but
in some cases an actual function call will be generated.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #12

P: n/a
In article <fv*********@news3.newsguy.com>,
Chris Torek <no****@torek.netwrote:
>The way to view this, to make sense out of it, is that the cast
operator is inherently made up of the token sequence '(',
type-name-keyword [%], ')'.
If you take this view, it makes more sense to say "... cast operators
are inherently made up of ...", since even if (int) is an operator,
it's clearly not the same operator as (double). The C standard seems
to do this except in the index, where "cast operator, ( )" points to
the section entitled "Cast operators".

-- Richard
--
:wq
Jun 27 '08 #13

P: n/a
In article <fv**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:
>When I write

(double)i;

I am conceptually calling

double TransformIntegerToDouble(int);
Why are you not conceptually calling Transform(int, double)?

-- Richard
--
:wq
Jun 27 '08 #14

P: n/a
Richard Tobin wrote:
In article <fv**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:
>When I write

(double)i;

I am conceptually calling

double TransformIntegerToDouble(int);

Why are you not conceptually calling Transform(int, double)?

-- Richard
Because the "int" is the RESULT type, not the input type!

Besides, a hyper hyper generic procedure:
Transform(anything,anything)
is just a dream...

You can have only concrete procedures:

Something TransformToSomething(something_else);

TransformIntegerToDouble exists (at least in lcc-win),
and will take care of the details, correct rounding, etc etc.

Transform(int,double) doesn't exist and can't exist since
it would have to take ANY two input types and transform the
second into the first, an impossible task!
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #15

P: n/a
In article <fv**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:

>Besides, a hyper hyper generic procedure:
Transform(anything,anything)
is just a dream...
Well, it's easily implementable:

#define Transform(value, type) ((type)(value))
>You can have only concrete procedures:
You were the one who said "conceptually":
>>I am conceptually calling

double TransformIntegerToDouble(int);
Conceptually, you can have whatever you like.

-- Richard
--
:wq
Jun 27 '08 #16

P: n/a
On 30 Apr 2008 13:10:55 GMT, Chris Torek <no****@torek.netwrote:
[% In C as originally defined by Dennis Ritchie, "typedef" did not
even exist. So all type names were keywords, which made parsing
C easy. All declarations were instantly obvious, as they always
AIUI very early (Labs/pre-K&R1) C allowed file-scope declarations with
no decl-specifiers at all, using implicit int AND implicit extern.
This was still easy enough for humans to read, and to parse.
started with a keyword, and since the total set of keywords was
small, everyone knew all of them at a glance. When typedef was
later added, some time in the late 1970s, the language became much
more complicated syntactically, since typedef identifiers are only
distinguishable from other identifiers by context. For instance,
the C89 -- but not C99 -- code fragment:

void f(x);

can mean either:

- declare f as function taking one argument of type int, and
returning void; or

- declare f as function taking one argument of type x, and
returning void.

The former makes use of C89's "implicit int"; the latter occurs if
and only if "x" is a typedef-name. <snip>
A function _declaration_ can't have an identifier-list; it's either
empty-parens () or a prototype. The difficulty occurs with a
definition, and isn't actually ambiguous; but, as you say ...
<snipFrom the parser's point of view, typedef-ed
identifiers are "just like" keywords, except when they are not.
If one attempts to use a conventional scanner and parser generator
like (f)lex and (b)yacc, one must invent clever feedback and/or
feed-forward mechanisms by which the parser can tell the lexer
whether to claim a potential typedef name is a "type name" or an
"identifier name". (Or one could use a more powerful parsing
technique, but that means abandoning yacc.)
Well, either the parser steers the lexer, or the lexer provides
(typically additional/different) information which allows the parser
to handle both possibilities. Either way it's a nuisance.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.