-5

in C

printf ("%d",sizeof ('a'));

output: 4

in C++

printf ("%d",sizeof ('a'));

output: 1 Why the outputs do not equal?

Fady Hany
  • 77
  • 6
  • In C, the type of a character constant is `int`, while in C++ it's `char`. – mediocrevegetable1 Aug 13 '21 at 04:22
  • 1
    Does this answer your question? [Size of character ('a') in C/C++](https://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c) This is only one of the many possible dupes found with [this search](https://www.google.com/search?client=firefox-b-d&q=sizeof+character+constant+different+in+c+and+c%2B%2B+site%3Astackoverflow.com). – mediocrevegetable1 Aug 13 '21 at 04:26
  • *Why the outputs do not equal?* -- Because the first one is `C` and the second one is `C++`. Now you know one reason why C and C++ are two different languages. – PaulMcKenzie Aug 13 '21 at 04:28
  • 1
    Side note: `%d` is not the correct specifier, `%zu` is. – Aykhan Hagverdili Aug 13 '21 at 05:01
  • Addendum: If you check the documentation for them, you'll find most of the helper functions don't take `char`s. They take `int`s. – user4581301 Aug 13 '21 at 05:19

2 Answers2

1

In C, the 'a' is considered int with a size of sizeof(int). While in C++, the 'a' is considered a char with a size of 1.

1

In C type of a char constant is a int while in C++ its a char