X
xcm
#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
why does it printf 2 2 in tc.
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
why does it printf 2 2 in tc.
xcm said:#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
why does it printf 2 2 in tc.
pete said:My guess is that sizeof(int) equals 2 in tc.
xcm said:#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
why does it printf 2 2 in tc.
A more better way of writing it would be
#include <stdio.h>
int main(void)
{
printf("%u %u\n", (unsigned)sizeof('a'), (unsigned)sizeof('A'));
return 0;
}
is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer
xcm said:is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer
thanks for the reply;
Your code is incorrect because the type returned byte sizeof is size_t.#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
why does it printf 2 2 in tc.
Why the cast? size_t is unsigned (see 7.1.6 of the Standard).
is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
Ah, this is a difference between using C++ and C. If sizeof('a')
equals 1 then you're using a C++ compiler, if it equals something
greater than 1 (when it returns the same value for sizeof(int)) then
xcm said:is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer
Paul Mesken wrote on 29/05/05 :
Bzzt. "greater or equal".
Paul Mesken wrote on 29/05/05 :
Nope. size_t is defined as an "unsigned integer" (according to the
target, could be an unsigned {char|short|int|long}).
unsigned integer != unsigned int.
Int might be the same size as char on the system (which is _extremely_
rare).
[email protected] said:| xcm wrote on 29/05/05 :
| > is 'a' an integer when use the sizeof?
| > why?
|
| 'a' is always an integer with type int in C. It's written on the
| standard. Don't ask why.
The standard allows for character literals with more than one character
also (like 'ab' or 'aaaa'). Don't ask why.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.