What does sizeof do without ()?

The author of this question just made me laugh when I asked him what sizeof * q does ... He told me that the really basic question is C, and I have to check this out. But when I looked at SO and didn’t see anyone asking before, I will do it myself now:

What does sizeof do in C when it is used without parentheses and / or type?

+7
c sizeof
source share
5 answers

So, if we look at the grammar for sizeof in the draft draft of C99 6.5.3 Unary operators, paragraph 1, it looks like this:

 sizeof unary-expression sizeof ( type-name ) 

There is a difference in which you cannot use the type name without parentheses and section 6.5.3.4 The sizeof operator of the statement specified in clause 2 says (emphasis mine):

The sizeof operator gives the size (in bytes) of its operand, which can be an expression or a name in type brackets . [...]

For completeness, you may wonder if we can use sizeof with an expression if we have parentheses, and the answer is yes, because a unary expression can contain parentheses. The easiest way to see this is to look at section A.2.1 Expressions from the draft standard and work with the grammar as follows:

 unary-expression -> postfix-expression -> primary-expression -> ( expression ) 
+20
source share

sizeof is an operator, not a function, so you don't need () if the operand is an expression (but you usually use them). sizeof * q == sizeof(*q) . Please note: if the operand is a type, you need to use () , so sizeof(int) (see, for example, http://en.wikibooks.org/wiki/C_Programming/Reference_Tables )

 sizeof sizeof operator for expressions sizeof 123 sizeof() sizeof operator for types sizeof (int) 

Now even the wiki is full of information about C ... The world is changing fast :-)

The sizeof operator is used to determine the amount of space that a particular data type will occupy in memory. To use sizeof, the keyword "sizeof" is followed by a type name or expression (which may simply be a variable name). If a type name is used, it should always be enclosed in parentheses, while expressions can be specified with or without parentheses.

+19
source share

sizeof is an operator, in particular a unary operator, such as unary & or ++ . It accepts either a unary expression or a type name surrounded by parens as its operand.

Syntax

  sizeof unary-expression 
   sizeof ( type-name )

So, given the type declaration

 int *q; 

the expressions sizeof *q and sizeof (int) equivalent, as are sizeof q and sizeof (int *) .

+7
source share

Same as sizeof with brackets. This is a right unary operator, so it treats its right size as an operand.
As far as I know, paranthesis are required when using sizeof with basic data types.

When you have an int a variable, you can check its size in both directions, brackets are optional: sizeof a , sizeof (a) will work the same.
But when checking int size only sizeof (int) will work.

+3
source share

The same thing happens with parens - it returns the size of what you pass it in, in this case the size of what q points to.

This is somewhat less clear, however, especially when we get involved in complex malloc and sizeof calls, so I think it's best practice to always use parens.

+2
source share

All Articles