"so char a='bcd'; is valid." No it is not, at least not in the sense you would expect (yes, 'bcd'
is a multibyte character constant, but that does not store multiple characters as you seem to expect, but is some implementation defined number).
A char
holds a single character, it is always one byte long. Writing char a
gives you one byte to play with. You cannot access the bytes before and after that via a
. The compiler also does not need to add any padding around your char
if it does not want to. Since the literal 'bcd'
has (at least on my system) a value that does not fit into a char
(6447972 on my system), you end up with a single one byte char
that has an implementation defined value. Even if there were padding bytes around a
, you cannot assume they get written to.
printf("%c\n",*(&a-1));
invokes undefined behavior since you do not own that memory. This has nothing to do with any kind of "word size".
If you want to store a sequence of characters use
const char *str = "Some text";
(or
char str[] = "Some text";
if you want to modify the sequence) in C or
std::string str = "Some text";
in C++.
Ok, your edit made it more clear what you are talking about. Quoting from what you posted:
4 ASCII characters can fit in a 32 bit integer
But a char
is not a 32 bit integer. It usually is an 8 bit integer. The text you posted is speaking about something like
int32_t a = 'bcd';
(or possibly uint32_t a
). Now a
is actually 32 bits long and can hold 32 bits of information. But as the text you quoted already says, this is rarely useful since the outcome of such an assignment is completely implementation dependent.