Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof operator error on data types

Tags:

c

I found it strange, that I am getting compile error when using sizeof int, while sizeof var (where var is a variable) works just fine.

int a;

a = (int) sizeof( a ); //ok
a = (int) sizeof( int ); //ok

a = (int) sizeof a; //ok
a = (int) sizeof int; //error

Can anyone tell me, why do I get the error?

(older machine, mac osx 10.5.8, gcc 4.0.1)

like image 510
notnull Avatar asked May 22 '26 16:05

notnull


1 Answers

According to the C standard (cited from ISO/IEC 9899:TC3 section 6.5.3.4):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

So using a type name without parenthesis isn't legal.

Also, sizeof returns an implementation defined value of the type size_t and you probably should not cast it.

like image 86
jpw Avatar answered May 25 '26 06:05

jpw