In C:
# Include <stdio. h>
Int main (){
Char A = 'a ';
Printf ("size of char: % d \ n", sizeof ());
Printf ("size of char: % d \ n", sizeof ('A '));
Return 0;
}
Output:
Size of char: 1
Size of char: 4
In C ++:
# Include <iostream>
Int main (){
Char A = 'a ';
STD: cout <"size of char:" <sizeof (a) <"\ n ";
STD: cout <"size of char:" <sizeof ('A') <"\ n ";
Return 0;
}
Output:
Size of char: 1
Size of char: 1
Why is the value returned by sizeof ('A') different from that returned by C ++?
According to the c99 standard, 'A' is called an integer character constant and is regarded as an int type. Therefore, it occupies 4 bytes on 32-bit machines.
According to the iso c ++ standard, 'A' is called character literal, which is regarded as a char type and occupies 1 byte.
C99:
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.
C ++ 2003:
A character literal is one or more characters enclosed in single quotes, as in 'X', optionally preceded by the letter L, as in L 'x '. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. an ordinary character literal that contains a single c-Char has type char, with value equal to the numerical value of the encoding of the C-char in the execution character set. an ordinary character literal that contains more than one C-Char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.