Size of character ('a') in C/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.
Difference between sizeof(char) and sizeof(char *)
char
is a character and sizeof(char)
is defined to be 1. (N1570 6.5.3.4 The sizeof and _Alignof operators, paragraph 4)
char*
is a pointer to a character and sizeof(char*)
depends on the environment. It is typically 4 in 32-bit environment and 8 in 64-bit environment.
In typical environment where sizeof(char*) > sizeof(char)
, malloc(sizeof(char*)*len + 1)
will (at least try to) allocate more memory than malloc(sizeof(char)*len + 1)
if len
is small enough not to cause integer overflow.
Why is sizeof a char literal not the same as sizeof(char)? [duplicate]
In C opposite to C++ integer character constants (literals) have the type int
.
So the value of the expression sizeof( 'a' )
is equal to the value of the expression sizeof( int )
. While sizeof( char )
is always equal to 1
.
From the C Standard (6.5.3.4 The sizeof and alignof operators)
4 When sizeof is applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the result is
1...
and (6.4.4.4 Character constants)
10 An integer character constant has type int. The value of an
integer character constant containing a single character that maps to
a single-byte execution character is the numerical value of the
representation of the mapped character interpreted as an integer. The
value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined. If an integer character constant contains a
single character or escape sequence, its value is the one that results
when an object with type char whose value is that of the single
character or escape sequence is converted to type int.
Pay attention to that usually objects of the type char
used in operations as operands or in expressions are converted to the type int
due to the integer promotions.
Why is the size of a character literal in C different than in C++ [duplicate]
In C, 'i'
has type int
for backwards-compatibility reasons. Thus sizeof('i')
shows the size of an int
on the chosen compilation platform.
In C++, because overloading made it more urgent to avoid giving surprising types to expression, it was decided to break backwards compatibility and to give 'i'
the type char
.
In C, why is sizeof(char) 1, when 'a' is an int?
In C 'a' is an integer constant (!?!), so 4 is correct for your architecture. It is implicitly converted to char for the assignment. sizeof(char) is always 1 by definition. The standard doesn't say what units 1 is, but it is often bytes.
Single, double quotes and sizeof('a') in C/C++
In C, character constants such as 'a'
have type int
, in C++ it's char
.
Regarding the last question, yes,
char ch = 'a';
causes an implicit conversion of the int
to char
.
sizeof('a') vs size('aa')
sizeof('a')
which is typeint
Neither one of 'a'
or sizeof('a')
is an int
. In C++, one-character literals are of type char
, and the type of a sizeof()
expression is size_t
.
Furthermore:
type
int
[...] should give me 4 bytes
No, int
need not necessarily be exactly 4 bytes long.
Related Topics
How to Enforce Move Semantics When a Vector Grows
Check If a Class Has a Member Function of a Given Signature
Best Practices For Circular Shift (Rotate) Operations in C++
Overloading Friend Operator≪≪ For Template Class
How Do Malloc() and Free() Work
Running My C++ Code Gives Me a Blank Console
What's the Correct Way to Use Printf to Print a Size_T
How to Serialize and Deserialize a Class in C++
Pass by Reference/Value in C++
What Is the Proper Declaration of Main in C++
Why Can't the Switch Statement Be Applied on Strings
What Are the Evaluation Order Guarantees Introduced by C++17
Where Are Static Variables Stored in C and C++
What Is External Linkage and Internal Linkage
Most Efficient Way to Find the Greatest of Three Ints