Data type of character constants in C and C++


In C++ the size of the character constants is char. In C the type of character constant is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.

Example

 Live Demo

#include<stdio.h>
main() {
   printf("%d", sizeof('a'));
}

Output

4

Example

 Live Demo

#include<iostream>
using namespace std;
main() {
   cout << sizeof('a');
}

Output

1

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

Updated on: 30-Jul-2019

630 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements