Why doesn't the following code report the size of '3' as 1 byte as '3' is a character and not an integer in this case??
Code:void main() { printf("%d",sizeof('3')); }
Why doesn't the following code report the size of '3' as 1 byte as '3' is a character and not an integer in this case??
Code:void main() { printf("%d",sizeof('3')); }
Single quotes indicate a single character.
Quzah.
Hope is the first step on the road to disappointment.
That's exactly what I meant. Single quotes should mean single charater and print out the size of '3' as 1 byte, but it inturn prints out 2..
Oh my.. a 2 line program with 3 errors in it...
1) It's not void main() ... it's int main (void) ... and yes it does matter.
2) Since main returns an int, you must return an errorlevel at the end of main... usually return 0; is adequate.
3) '3' is a character literal, it's stored in the program executable image... sizeof() is returning the size of the *pointer* to the location where it's stored.
2??? It should print 4 ... unless you are on some crappy pre-flintstone 16 bit compiler like Turbo C...
It sizeof(pointer) is returning 2 you seriously need to update your compiler... I generally suggest Pelles C
Double quotes (a string) would give you a string literal, but since he has single quotes, I believe he's just getting an integer value. (The integer value of the character 3. Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion)Code:printf( "%c %d", '3', '3' );
Quzah.
Hope is the first step on the road to disappointment.
Hmmm... I think you may be right... string literalls are usually stored in the data segment of the PE image, but it would be simple as pie to store character literals as ints right in the code... Or might this be an implementation defined thing?
In any case 2 is a disastrous answer for our friend...
It reports 2 bytes with Turbo C, and 1 byte with Micrsoft Visual C++ express edition.
In C, '3' is an int. Any character constant (with a single character) has type int, not char, so sizeof('3') is the same as sizeof(int). However, C++ fixed/changed this, so character constants have type char. That's why you get a difference.It reports 2 bytes with Turbo C, and 1 byte with Micrsoft Visual C++ express edition.
Leave it to microsoft to do things their own --very unusual-- way.
I get 4 back from Pelles C which is C-99 standards compliant.
Hi Juice,
i fell that '3' is treat as integer in sizeof('3'). wt you are expecting output -1 will get if you type cast that one. like sizeof( (char) ('3'));
Hi Juice,
i fell that '3' is treat as integer in sizeof('3'). wt you are expecting output -1 will get if you type cast that one. like sizeof( (char) ('3'));
Actually, no, cas is correct. It is true that '3' is a character literal. However, it is not true that "sizeof() is returning the size of the *pointer* to the location where it's stored". Rather, the result of sizeof is the size of '3', which is equivalent to sizeof(int) since character literals are of type int.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> Leave it to microsoft to do things their own --very unusual-- way.
Microsoft did nothing wrong - it's purely the difference between C and C++.
Incompatibilities Between ISO C and ISO C++
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.