2

I have read a post on C before here on SO. I have forgotten what it was about, but definitely was not about what I would like to ask here - sizeof.

It was argued between 2 answerers in the comment section. It was a heated debate between the two but an explanation was never explicitly uttered.

I have tried to find it for two days, but I have not been able to locate it, hence my question here. And Googling didn't generate any result regarding this.

Focus: sizeof() or sizeof () or just sizeof

So there are basically 2 types of these people regarding this:

First group:

A scholar who swears by the name of Ritchie and never will drive a car because he tries to register his license plate with:

sizeof () with a space between sizeof and () and the government (in some countries this - () - is allowed in license plates) won’t allow the empty space between numbers/letters.

Second group:

Every once in a while a decorated C veteran with a tie-dyed shirt who would reply you like so when asked: „I really don’t know man, what’s the difference? I don’t even know what the deal is. I don't even remember.. Those Ivy Lea…. sizeof(), sizeof () or just sizeof, me no worry, man. “

So, should sizeof be written with a space between sizeof and its () or even without the () or really it doesn’t matter?

I welcome explanations from C experts and scholars. Those who don’t quite fit into the aforementioned types - the self-taught C Bobs and Marys - with „C99 and Ritchie“ tattooed on his/her arm are also welcomed.

It doesn’t hurt me either ways but I am just curious whether or not if it is a „standard“ to put a space between sizeof and () or even one without ().

Or are they really all the same - would be interesting to me.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Unheilig
  • 16,196
  • 193
  • 68
  • 98

3 Answers3

7

All yield the same result and which you use is entirely down to preference.

Note that you must use parens if you are supplying a type name. More detail on that can be found here: What does sizeof without () do?

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 5
    Personally I always use the parens just after the "sizeof" with no space. This follows most function calling conventions and thinking of "sizeof" as a function isn't harmful (IMHO) – drew_w Jan 11 '14 at 16:59
5

No difference in sizeof() and sizeof (). But there is a little difference in sizeof and sizeof (). The standard says about it:

6.5.3 Unary operators:

sizeof unary-expression
sizeof ( type-name )   

If the operand is type name, then () is needed. Otherwise, for unary-expressions you can use it without ().

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Quick review of the C spec for examples:

  1. () after sizeof is not used when not needed.

    sizeof *dp
    
  2. A space is used between sizeof and () otherwise (as in with a type name).

    sizeof (struct ss)
    

Obvious the 3 styles by the OP are all allowed and the spec does not mandate any 1 of the 3 (aside from () with a type name).

IMHO, I concur with @drew_w comment about sizeof().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • "a space is used between sizeof and () otherwise...".. because? – Unheilig Jan 11 '14 at 19:11
  • 1
    @Unheilig §6.5.3 specifies 2 uses of the `sizeof` operator: `sizeof` _unary-expression_ and `sizeof (` _type-name_ `)`. Why the C spec examples used the style `sizeof[space](struct ss)` instead of `sizeof[nospace](struct ss)` is something I could only at best conjecture: committee compromise. – chux - Reinstate Monica Jan 11 '14 at 19:25
  • 1
    You can 0 or more spaces – David Heffernan Jan 11 '14 at 20:43
  • 1
    @David Heffernan Certainly one can use 0+ spaces between `sizeof` and `()`. The C spec is not obliging code use a given number of spaces. Still the spec does have a uniform style in its _examples_ that is 1 space and I thought that was worth noting to this post. – chux - Reinstate Monica Jan 11 '14 at 20:58