Data Type of Character Constants in C and C++



The character constant is a constant that is enclosed in a single quote (' '). It represents the integer value (i.e., ASCII) of the character. In C programming language, the data type of character constant is an integer (represented by int) and it needs 4 bytes to store a character constant.

For example: 'A', '1', '3', etc., are the character constants.

In C++, the data type for character constants is char and it needs only 1 byte to store a character constant.

Here are the size of different data types in bytes:

Data Types Size (in byte)
char 1 byte
int 4 bytes
float 4 bytes
double 8 bytes
long 4 or 8 bytes
long long 8 bytes

As we have the table of size (in bytes) of each data type in C and C++, we can easily compare the size of any character constant with the known data type size and can easily recognize which data type it belongs.

Checking Data Type of Character Constant

In both C and C++, you can determine the type of a character constant by checking its size. If the character constant occupies 1 byte, it is of type char. If it occupies 4 bytes, it is treated as an int. To get the size of character constant (or, any variable), use the sizeof() operator that returns the number of bytes occupied by a variable/value.

Example

The following C and C++ examples demonstrate how you can check the data type for character constants using the sizeof() operator:

C C++
#include <stdio.h>

int main() {
    const char ch = 'A';
    printf("The given character constant is: %c\n", ch);
    printf("The size of the character constant 'A' is: %ld bytes\n", sizeof('a'));
}

Following is the output to the above program:

The given character constant is: A
The size of the character constant 'A' is: 4 bytes
#include <iostream>
using namespace std;

int main() {
    const char ch = 'A';

    cout << "The given character constant is: " << ch << endl;
    cout << "The size of the character constant " << ch << " is: " << sizeof(ch) << " bytes";

    return 0;
}

Following is the output to the above program:

The given character constant is: A
The size of the character constant A is: 1 bytes

In both cases we are doing the same. But in C sizeof(ch) returns 4 as it is treated as integer. But in C++ it is returning 1. It is treated as character.

Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-05-30T17:31:42+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements