Size of character ('a') in C/C++

C++CTypes

C++ Problem Overview


What is the size of character in C and C++ ? As far as I know the size of char is 1 byte in both C and C++.

In C:

#include <stdio.h>
int main()
{
    printf("Size of char : %d\n", sizeof(char));
    return 0;
}

In C++:

#include <iostream>
int main()
{
    std::cout << "Size of char : " << sizeof(char) << "\n";
    return 0;
}

No surprises, both of them gives the output : Size of char : 1

Now we know that characters are represented as 'a','b','c','|',... So I just modified the above codes to these:

In C:

#include <stdio.h>
int main()
{
    char a = 'a';
    printf("Size of char : %d\n", sizeof(a));
    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 the sizeof('a') returns different values in C and C++?

C++ Solutions


Solution 1 - 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.

Solution 2 - C++

As Paul stated, it's because 'a' is an int in C but a char in C++.

I cover that specific difference between C and C++ in something I wrote a few years ago, at: http://david.tribble.com/text/cdiffs.htm

Solution 3 - C++

In C the type of character literals are int and char in C++. This is in C++ required to support function overloading. See this example:

void foo(char c)
{
    puts("char");
}
void foo(int i)
{
    puts("int");
}
int main()
{
    foo('i');
    return 0;
}

Output:

char

Solution 4 - C++

In C language, character literal is not a char type. C considers character literal as integer. So, there is no difference between sizeof('a') and sizeof(1).

So, the sizeof character literal is equal to sizeof integer in C.

In C++ language, character literal is type of char. The cppreference say's:

> 1) narrow character literal or ordinary character literal, e.g. 'a' or > '\n' or '\13'. Such literal has type char and the value equal to the > representation of c-char in the execution character set. If c-char is > not representable as a single byte in the execution character set, the > literal has type int and implementation-defined value.

So, in C++ character literal is a type of char. so, size of character literal in C++ is one byte.

Alos, In your programs, you have used wrong format specifier for sizeof operator.

C11 §7.21.6.1 (P9) :

> If a conversion specification is invalid, the behavior is > undefined.275) If any argument is not the correct type for the > corresponding conversion specification, the behavior is undefined.

So, you should use %zu format specifier instead of %d, otherwise it is undefined behaviour in C.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwhacko__CrackoView Question on Stackoverflow
Solution 1 - C++anonView Answer on Stackoverflow
Solution 2 - C++David R TribbleView Answer on Stackoverflow
Solution 3 - C++KiteView Answer on Stackoverflow
Solution 4 - C++mscView Answer on Stackoverflow