×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

What is the value of sizeof('x') and type of character literals in C++?

In C programming language, the type character literals was int so there was no difference between sizeof('x') and sizeof(10). Hence, C language considers character literal as integer (ASCII of the character).

Example

Let's consider the following example

#include <stdio.h>
int main()
{
	printf("%d,%d\n",sizeof('x'),sizeof(10));
	return 0;
}
    4,4

The output will be 4,4 (On 32 bits system architecture)

Type of character literal in C++

In C++ programming language, the type of character literal is changed from int to char. That means sizeof('x') and sizeof(10) is different.

Example

Let's consider the following example

#include <iostream>
using namespace std;

int main()
{
	cout<<sizeof('x')<<","<<sizeof(10)<<endl;
	return 0;
}
    1,4

The output will be 1,4 (On 32 bits system architecture)

What's the difference?

In C programming language character literal is integer type so sizeof('x') will be 4, and in C++ programming language character literal is character type so sizeof('x') will be 1.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.