Character (and string) literals in program code are somehow translated by the compiler into a sequence of bytes. The rule of this transformation depends on the encoding of the source code (as other participants have already mentioned), but may also depend on a number of factors (see the answer to another question).
It should be noted that both versions of your code when trying to compile with the clang compiler lead to an error :
error: character too large for enclosing character literal type char b = 'ф'; /// кириллица
And the gcc you use gives you a couple of warnings for the line with the letter:
warning: multi-character character constant [-Wmultichar] warning: overflow in implicit constant conversion [-Woverflow]
The first one talks about using a multi-character literal (which is not supported by all compilers). The second is that this literal does not fit into char . Those. the 'ф' type was interpreted by the compiler as something more than char , and, as already mentioned in the quotation from the @Harry answer, this type is int :
... has a type int , and has an implementation-defined value.
Based on the foregoing, we can conclude:
4 (example 1) is obtained because the multi-character literal is not truncated and its size is equal to the size of int , i.e. sizeof(int) == 4 .1 (example 2) is obtained because the multi-character literal was truncated to type char when the variable b initialized, and sizeof(char) == 1 by definition.
I will reply here and to your comment on another post:
I imagined a multibyte literal as one character from a complex encoding, let's say 'f' (UTF-8) and this is 2 bytes, we can write no more than 2 'f' by your words ... inconsistent with 4 characters
Record more than 2 'ф' you really will not work .
#include <stdio.h> int main() { printf("\n%d",sizeof('ффф')); }
warning: character constant too long for its type
Those. in fact, truncation of the value to sizeof(int) occurs.
But if a string literal is used, then two bytes can be enough for storing ф :
#include <stdio.h> int main() { const char c[] = "ф"; printf("%ld\n", sizeof(c)); }
3
Displays the number 3 , because 1 byte is allocated for terminating zero.
sizeofis not a function, but an operator. Yes, and who taught you the string to start with '\ n'? - 0andriy