0

When I run the program below in C, I get the output result to be 4.

#include <stdio.h>

//using namespace std;

int main()
{
    printf("%d", sizeof('a')); 
  return 0; 
}

But when I run the code below in C++, I get the output result to be 1.

#include <iostream>

using namespace std;

int main()
{
    printf("%d", sizeof('a')); 
  return 0; 
}

Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?

Kundan
  • 1,754
  • 16
  • 37

1 Answers1

0

In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.

In C++, it's of a character type.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261