4 characters are not being put into a char
variable, but into an int
character constant which is then assigned to a char
.
3 parts of the C standard (C11dr §6.4.4.4) may help:
"An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'."
"An integer character constant has type int
."
"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."
OP's code of char ch='abcd';
is the the assignment of an int
to a char
as 'abcd'
is an int
. Just like char ch='Z';
, ch
is assigned the int
value of 'Z'
. In this case, there is no surprise, as the value of 'Z'
fits nicely in a char
. In the 'abcd'
, case, the value does not fit in a char
and so some information is lost. Various outcomes are possible. Typically on one endian platform, ch
will have a value of 'a'
and on another, the value of 'd'
.
The 'abcd'
is an int
value, much like 12345
in int x = 12345;
.
When the size(int) == 4
, an int
may be assigned a character constant such as 'abcd'
.
When size(int) != 4
, the limit changes. So with an 8-char int
, int x = 'abcdefgh';
is possible. etc.
Given that an int
is only guaranteed to have a minimum range -32767 to 32767, anything beyond 2 is non-portable.
The int
endian-ness of even int = 'ab';
presents concerns.
Character constant like 'abcd'
are typically used incorrectly and thus many compilers have a warning that is good to enable to flag this uncommon C construct.