Is sizeof technically a function?

May 31, 2014 at 2:17pm
Hi all

In pure C,
Would this be valid?:
char testCh[sizeof ( int )];

I saw somewhere, someone saying that sizeof wasnt technically a function, so its not performed at runtime, or possibly before compilation?

Does this mean that sizeof could be considered a constant for the use above?

Many Thanks,
Super_Stinger
May 31, 2014 at 2:34pm
sizeof is operator in C/C++ language and is calculated in compile time.
And yes, you can use it everywhere where you need compile-time constant, like array size.

BTW, parenteses are unnessesary: char testCh[sizeof int]
As Smac89 pointed out, you do need parentheses when you are applying sizeof to types. You can use it without them on variables:
1
2
int a;
char testCh[sizeof a];
Last edited on May 31, 2014 at 6:22pm
May 31, 2014 at 6:15pm
@MiiNiPaa, in this case, the parentheses are needed
May 31, 2014 at 11:59pm
Awesome!

Saves me from writing programs that 'assume' that an int is 4 bytes ( I guess its common practice to make programs more portable )

Cheers Guys, you are awesome! :)
Jun 1, 2014 at 7:04am
@ SuperStinger: don't forget about stdint.h.

http://www.cplusplus.com/reference/cstdint/
Topic archived. No new replies allowed.