#include <stdio.h>
int main(void)
{
printf("sizeof(char) = %zu\n", sizeof(char));
printf("sizeof('a') = %zu\n", sizeof('a'));
}
See https://godbolt.org/z/1eThqvMhx
When running this code in C, it prints
sizeof(char) = 1
sizeof('a') = 4
When running this code in C++, it prints
sizeof(char) = 1
sizeof('a') = 1
Why does the output differ between languages?
What is the size of a character in C and C++? As far as I know, the size of char
is 1 byte in both C and C++.
In C, the type of a character constant like 'a'
is actually an int
, with size of 4 (or some other implementation-dependent value). In C++, the type is char
, with size of 1. This is one of many small differences between the two languages.