Saturday, December 30, 2006

sizeof()

Well found something new about the sizeof()

firstly it is an unary operator and shouldn't be mistaken for a function

secondaly, what do you expect the output of
int i=10;
j=sizeof(i++);
printf("%d, %d",i,j);

well the value of i doesn't get incremented as sizeof is a compile time operator and produces a compile-time integer constant value. The expession inside sizeof() is only expected to deduce its type and is not fully evaluated.... should have known this though!!!!

also found a new thing today.... sizeof('x') returns 2 or 4 but not 1. This is because by definition, in C, character constants are integers and not characters!!!! I also found this somewhere that sizeof("") will give the size of char in both C and C++, but haven't tried it yet.

Also,
char chr[1000];
sizeof(chr) will give 1000 and not 4.

No comments: