"Akhil" <> writes:
> Since a character constant is an int value represented as a character
> in single quotes,so it is treated as a 1 byte integer now look at the
> following snippet.
A character constant is of type int; for example,
sizeof('x') == sizeof(int). If sizeof(int) happens to be 4,
a character constant specifies a 4-byte value.
> #include<stdio.h>
> int main(void)
> {
> char a='Abbc';
> printf("%c",a);
>
> return 0;
> }
>
> Please explain how the initialization is interpreted,on compilation it
> gives a warning "multi-character character constant" but runs without
> error also possibly the last character within the single quotes is
> being stored in a.
C99 6.4.4.4p10:
The value of an integer character constant containing more than
one character (e.g., 'ab'), or containing a character or escape
sequence that does not map to a single-byte execution character,
is implementation-defined.
So 'Abbc' has some implementation-defined value of type int. Using
that value to initialize a variable of type char requires a conversion
from int to char. If char happens to be unsigned, the conversion is
well defined; if char is signed, the result of the conversion is
implementation-defined (or it can raise an implementation-defined
signal, but that's not likely).
Try changing a from a char to an int and displaying the value in
hexadecimal:
int a = 'Abbc';
printf("a = 0x%x\n", a);
On one implementation, I get
a = 0x41626263
which corresponds to the ASCII values of the individual characters
'A', 'b', 'b', and 'c'. Assigning that value to a char happens to
give you the low-order 8 bits, the value of 'c'. All of this,
including the use of ASCII, is implementation-defined; you shouldn't
depend on any of it if you care at all about portability.
Incidentally, you need a '\n' after you print the value; otherwise
your output may not appear.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.