
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Type difference of character literals in C vs 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
#include<stdio.h> main() { printf("%d", sizeof('a')); }
Output
4
Example
#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.
- Related Articles
- Type difference of character literals in C and C++
- Character constants vs String literals in C#
- Integer literals vs Floating point literals in C#
- What is the difference between character literals and string literals in Java?
- What are Character Literals in C++?
- What is the type of string literals in C/ C++?
- What is the type of string literals in C and C++?
- Object literals vs constructors in JavaScript
- Value Type vs Reference Type in C#
- Data type of character constants in C and C++
- Literals in C#
- C Program to check the type of character entered
- Compound Literals in C
- Octal literals in C
- C program for testing the character type

Advertisements