Short answer
Yes. In C, sizeof(char) is always 1. The size of a char is the unit that sizeof counts in. What is not fixed is how many bits make up that unit. The number of bits is set by CHAR_BIT, and on most current systems it is 8.
What sizeof(char) means
The C standard says that sizeof applied to a char, signed char, or unsigned char gives 1. So sizeof(char) is 1 on every conforming system, whatever the hardware. The C++ standard gives the same rule.
#include <stdio.h>
int main(void) {
printf("sizeof(char) = %zu\n", sizeof(char)); // always 1
return 0;
}
The %zu format is for size_t, the type that sizeof returns.
How many bits a byte has
The number of bits in a char is given by CHAR_BIT. It is defined in <limits.h> in C and in <climits> in C++. The standard requires at least 8 bits. Most common desktop, server, and mobile systems use 8, but a platform is allowed to use more.
So in C, one byte is the size of one char. On most systems that is 8 bits, but the standard does not fix it at exactly 8.
A char is not always one character of text
A char holds one code unit, which is not always one visible character. In UTF-8, the letter é is stored as two bytes, so it takes two char values. A string that looks short on screen can have more char values than it has letters.
Because of this, the length of a string in char values is not always the number of characters a reader sees.
Other sizes that vary
Some related sizes depend on the platform:
intis not always 4 bytes. Four bytes is common on mainstream systems, but the standard sets only minimum ranges forint. If your code depends on the size ofint, check it withsizeofinstead of assuming four bytes.wchar_tis used for wide characters, and its size depends on the platform.- Whether plain
charis signed or unsigned depends on the compiler. signed vs unsigned in c explains what that changes.
The sizes of the other basic types are listed in primitive data types in c. How sizeof itself works is covered in sizeof function in c.
Checking the sizes on your system
This program prints three sizes. The values can differ from one system to another:
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("sizeof(char) = %zu\n", sizeof(char));
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("sizeof(int) = %zu\n", sizeof(int));
return 0;
}
On many common desktop systems, the output is 1, 8, and 4. The first line always shows 1. The other two depend on the platform. A character’s numeric code is a separate topic, covered in c char to int.