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.


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.