sizeof without parenthesis

H

Howard Bryce

I have come across code containing things like

sizeof int

How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?
 
R

Richard Heathfield

Howard Bryce said:
I have come across code containing things like

sizeof int

which won't compile. The operand of sizeof is either an expression or a
parenthesised type.
How come that one can invoke sizeof without any parentheses surrounding
its argument?

It's not an argument, because sizeof is not a function. It's an operator,
taking an operand.
Is this admissible within the standard?

Yes, of course.
Can it in general be done with one argument functions?

No, but sizeof is not a function.
Why would one want to do it anyway,
other than showing that one knows and to save two characters?

(Why (would (one (want (to (add (spurious (parentheses)))))))?)
 
M

Michael Mair

Howard said:
I have come across code containing things like

sizeof int

This is almost certainly not true.
You may have come across things like
sizeof myVar
How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

sizeof is an operator, not a function.
Its argument either is a type name in parentheses or some expression.
Example:

The prefered way to use malloc() in comp.lang.c is
T *p;
....
p = malloc(sizeof *p);
if (p == NULL) {
/* your error handling here */
}
or
p = malloc(number * sizeof *p);
rather than
p = malloc(sizeof (T));
or
p = malloc(number * sizeof (T));
because changing the type of p (and *p, respectively) necessitates
no adaption of the argument of malloc().

Some people are afraid that unparenthesised sizeof operands may
bite them or are afraid that they get the "precedence" wrong, thus
they always write "sizeof (something)".


Cheers
Michael
 
K

Keith Thompson

Howard Bryce said:
I have come across code containing things like

sizeof int

That particular form is illegal.
How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

"sizeof" is not a function, it's a built-in unary operator, like "&"
or "*". It just happens to be the only operator whose symbol is a
keyword rather than a punctuation mark.

The syntax is:

sizeof unary-expression
sizeof ( type-name )

For example, you can write "sizeof 42"; 42 is an expression, and
"sizeof 42" yields the size of that expression's type (int).

You can also write "sizeof(42)" if you like. In this case, the
parentheses aren't related to the sizeof operator; you're merely
applying "sizeof" to a parenthesized expression. It's exactly the
same thing as "-42" vs. "-(42)".

Unlike other operators, sizeof can also be applied to a parenthesized
type name, as in "sizeof(int)". "sizeof int", without the
parentheses, is a syntax error. The parentheses in "sizeof(int)" are
not syntactically related to the parentheses in a function call (or to
the parentheses in a cast operation); they're merely part of the
syntax of the sizeof operator.

(Function calls always require parentheses.)
 
A

Ancient_Hacker

Howard said:
I have come across code containing things like

sizeof int

How come that one can invoke sizeof without any parentheses surrounding
its argument?

Well, I guess K & R ran out of ascii characters. Every dang printing
character except for backquote and at sign had already been used for
something or nother, usually as a unary or binary operator.

You see, "sizeof( foo )" may LOOK like a function call, but it's not.
Mainly because "sizeof'" has to be determined at compile time.
Otherwise it cvouldnt be used in many contexts where the compiler
requires a constant or expression that can be completely evaluated at
compile-time. So "sizeof" is a lot more like ampersand, or tilde, or
unary minus--, it does something to the thing following it. In
particular, it returns the "size" of whatever follows.

If just one little neuron had fired, maybe K & R would have made
at-sign the "sizeof unary operator and a whole lot of discusions on
this N.G. would have never materialized-- an alternate and not at all
horrific divergent reality. Lets see: i = @Array; Hmmm, not any
whole lot uglier than any other C construct. And nobody hardly would
be asking "What the heck is with this sizeof thingy anyway?".
 
W

Walter Roberson

Ancient_Hacker said:
Howard Bryce wrote:
Well, I guess K & R ran out of ascii characters. Every dang printing
character except for backquote and at sign had already been used for
something or nother, usually as a unary or binary operator.

You missed dollar sign. Possibly it wasn't used because some closely
related standards use that position as the "national currency symbol"
(such as pounds stirling).

I do not have any hypothesis about backquote -- none that wouldn't
also involve {|} .
If just one little neuron had fired, maybe K & R would have made
at-sign the "sizeof unary operator and a whole lot of discusions on
this N.G. would have never materialized-- an alternate and not at all
horrific divergent reality. Lets see: i = @Array; Hmmm, not any
whole lot uglier than any other C construct. And nobody hardly would
be asking "What the heck is with this sizeof thingy anyway?".


Did the use of @ as a rubout character (teletype) happen to proceed
the development of C?
 
M

Michael Mair

Ancient_Hacker said:
Well, I guess K & R ran out of ascii characters. Every dang printing
character except for backquote and at sign had already been used for
something or nother, usually as a unary or binary operator.

$ comes to mind...
You see, "sizeof( foo )" may LOOK like a function call, but it's not.
Mainly because "sizeof'" has to be determined at compile time.
Otherwise it cvouldnt be used in many contexts where the compiler
requires a constant or expression that can be completely evaluated at
compile-time. So "sizeof" is a lot more like ampersand, or tilde, or
unary minus--, it does something to the thing following it. In
particular, it returns the "size" of whatever follows.

If just one little neuron had fired, maybe K & R would have made
at-sign the "sizeof unary operator and a whole lot of discusions on
this N.G. would have never materialized-- an alternate and not at all
horrific divergent reality. Lets see: i = @Array;

Looks like Perl ;-)
Hmmm, not any
whole lot uglier than any other C construct. And nobody hardly would
be asking "What the heck is with this sizeof thingy anyway?".

True. And they might have done a lot of other things differently
which also would have made life easier for us.


Cheers
Michael
 
A

Ancient_Hacker

Walter said:
Did the use of @ as a rubout character (teletype) happen to proceed
the development of C?

Precede, and no I don't think so.

The @ as line erase I first saw in Unix. Having a rubout character on
a shift key seems particularly inelegant, although some older terminals
(GE Terminet>??) may have had a lower case atsign near the return key.
 
K

Keith Thompson

Michael Mair said:
$ comes to mind...
[...]

At the time, I think a lot of non-US keyboards didn't have the '$'
character. (Of course, they later ran into problems with other
characters that weren't universally available, thus trigraphs.)
 
R

Richard Tobin

Why would one want to do it anyway,
other than showing that one knows and to save two characters?
[/QUOTE]
(Why (would (one (want (to (add (spurious (parentheses)))))))?)

Well, you could ask mathematicians, who sometimes write f x and
sometimes write f(x). Presumably they'd tell you that they write
whatever seems clearest at the time.

-- Richard
 
J

Jordan Abel

Did the use of @ as a rubout character (teletype) happen to proceed
the development of C?

from what i've heard, it's originally a multics thing, so yeah, but the
same applies to #. Which some of the same "closely related standards"
that lack dollar sign sometimes use for something else - like a pound
sign. It's really hard to justify excluding _any_ of those characters,
in the historical context, while allowing { | } \ [ ] ^ _ ~ but maybe
it's an ebcdic thing.
 
K

Keith Thompson

Ancient_Hacker said:
Well, I guess K & R ran out of ascii characters. Every dang printing
character except for backquote and at sign had already been used for
something or nother, usually as a unary or binary operator.

You see, "sizeof( foo )" may LOOK like a function call, but it's not.
Mainly because "sizeof'" has to be determined at compile time.
Otherwise it cvouldnt be used in many contexts where the compiler
requires a constant or expression that can be completely evaluated at
compile-time. So "sizeof" is a lot more like ampersand, or tilde, or
unary minus--, it does something to the thing following it. In
particular, it returns the "size" of whatever follows.

If just one little neuron had fired, maybe K & R would have made
at-sign the "sizeof unary operator and a whole lot of discusions on
this N.G. would have never materialized-- an alternate and not at all
horrific divergent reality. Lets see: i = @Array; Hmmm, not any
whole lot uglier than any other C construct. And nobody hardly would
be asking "What the heck is with this sizeof thingy anyway?".

You're assuming that operator names *should* always be punctuation
marks. That's a valid opinion, but not one that I share. There's
nothing wrong with using punctuation marks for some operators, and
keywords for others. (For example, some other languages use "div",
"mod", and/or "rem" for integer division.)

What sometimes causes confusion in C is that "sizeof" is the *only*
operator represented by a keyword. I certainly wouldn't suggest that
"+" and "-" shouldn't be used for addition and subtraction, but
perhaps if a few other operators were represented as keywords,
"sizeof" wouldn't cause so much confusion.

But once you understand that "sizeof" is an operator, and that you
have to understand the specific rules for its use, it's really not all
that complicated.
 
J

jmcgill

Howard said:
I have come across code containing things like

sizeof int

How come that one can invoke sizeof without any parentheses surrounding
its argument?

sizeof is not a function, it is an operator. It is somewhat important
to understand the distinction.
Is this admissible within the standard? Can it in general be
done with one argument functions?

sizeof is not a one-argument function, it is a unary operator.
Why would one want to do it anyway,
other than showing that one knows and to save two characters?

It helps distinguish the fact that sizeof is not a function, it is an
operator.

When sizeof is used with a type name, the parentheses are required.

It is not incorrect to use parentheses in any use of sizeof, because it
introduces no ambiguity, but it is not required to use parentheses with
a variable or expression.
 
T

T.M. Sommers

Walter said:
Did the use of @ as a rubout character (teletype) happen to proceed
the development of C?

It certainly wasn't used on regular teletypes. When using
5-level Baudot/Murray code, rubout was all marks, or
letters-shift. I think it was all marks when using 8-level code,
too, and I think the ASR-33 had a special rubout key, but I could
be wrong.
 
P

Paul Connolly

(Why (would (one (want (to (add (spurious (parentheses)))))))?)
perhaps they might be lisp programmers?
(after years of jokes like this the newer functional programming languages
(like ml) don't even have brackets around the arguments to a function - they
don't even have commas to delimit arguments).
 
J

Joe Wright

Howard said:
I have come across code containing things like

sizeof int

How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

<rant>
How come you need to crowd this valuable space with questions to which
you should already know the answer. Your C reference will tell you what
sizeof is all about and how you can use it.

If you want to know WHY it's that way, you're in the wrong group. Maybe
comp.std.c or something.
</rant>

The sizeof operator is a compiler primitive and not particularly smart.
Its default operand is the name of an object. Types may be more
complicated than simple names and so we give the compiler a clue with
parentheses that its argument is a (user defined?) type.

#include <stdio.h>

typedef struct stru {
int a;
int b;
int c;
} stru;

size_t size;
int i;
stru s;

int main(void) {
size = sizeof s;
printf("Size is %lu bytes.\n", size);
size = sizeof (stru);
printf("Size is %lu bytes.\n", size);
size = sizeof i;
printf("Size is %lu bytes.\n", size);
size = sizeof (int);
printf("Size is %lu bytes.\n", size);
return 0;
}

From the style side, note that sizeof is an operator, not a function.

size = sizeof(int); /* Ugly as sin */

size = sizeof (int); /* Beauty */
 
C

Christopher Layne

Joe said:
From the style side, note that sizeof is an operator, not a function.

size = sizeof(int);  /* Ugly as sin */

size = sizeof (int); /* Beauty */

Oh please. One is not more uglier than the other. It's a frickin' space for
christ's sake.
 
K

Keith Thompson

Joe Wright said:
<rant>
How come you need to crowd this valuable space with questions to which
you should already know the answer. Your C reference will tell you
what sizeof is all about and how you can use it.

If you want to know WHY it's that way, you're in the wrong
group. Maybe comp.std.c or something.
</rant>

The sizeof operator is a compiler primitive and not particularly
smart. Its default operand is the name of an object. Types may be more
complicated than simple names and so we give the compiler a clue with
parentheses that its argument is a (user defined?) type.

There is no "default operand" for the sizeof operator. If you write
"sizeof" with no operand it's a syntax error; the compiler won't fill
one in for you.

The operand of sizeof can be (nearly) any expression or a
parenthesized type name; specifically, the grammar is:

sizeof unary-expression
sizeof ( type-name )

with the following constraints:

The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of
such a type, or to an expression that designates a bit-field
member.

In particular, it needn't be the name of an object; "sizeof 42" is
legal, and yields the same result as "sizeof (int)".

[...]
From the style side, note that sizeof is an operator, not a function.

size = sizeof(int); /* Ugly as sin */

size = sizeof (int); /* Beauty */

I hadn't thought about this before, but I like it. In the past, I've
tended to write "sizeof(int)", but I agree that "sizeof (int)" is
better. It's not a function call, so it shouldn't look like one.
Thanks!
 
R

Richard Heathfield

Keith Thompson said:
I hadn't thought about this before, but I like it. In the past, I've
tended to write "sizeof(int)", but I agree that "sizeof (int)" is
better. It's not a function call, so it shouldn't look like one.

You kill me, Keith, you really do. I responded along precisely those lines.
Go get your /own/ set of reactions, you plagiarist!
 
C

CBFalconer

Richard said:
Keith Thompson said:

You kill me, Keith, you really do. I responded along precisely
those lines. Go get your /own/ set of reactions, you plagiarist!

Does this mean you've finally kicked the habit of writing if, for,
while etc. as if they were functions?

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top