Home » C++ programming language

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).

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.

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.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.
Close