Is sizeof technically a function?

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
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
@MiiNiPaa, in this case, the parentheses are needed
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! :)
@ SuperStinger: don't forget about stdint.h.

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