58

I was looking at the question Single quotes vs. double quotes in C or C++. I couldn't completely understand the explanation given so I wrote a program:

#include <stdio.h>
int main()
{
  char ch = 'a';
  printf("sizeof(ch) :%d\n", sizeof(ch));
  printf("sizeof(\'a\') :%d\n", sizeof('a'));
  printf("sizeof(\"a\") :%d\n", sizeof("a"));
  printf("sizeof(char) :%d\n", sizeof(char));
  printf("sizeof(int) :%d\n", sizeof(int));
  return 0;
}

I compiled them using both gcc and g++ and these are my outputs:

gcc:

sizeof(ch)   : 1  
sizeof('a')  : 4  
sizeof("a")  : 2  
sizeof(char) : 1  
sizeof(int)  : 4  

g++:

sizeof(ch)   : 1  
sizeof('a')  : 1  
sizeof("a")  : 2  
sizeof(char) : 1  
sizeof(int)  : 4  

The g++ output makes sense to me and I don't have any doubt regarding that. In gcc, what is the need to have sizeof('a') to be different from sizeof(char)? Is there some actual reason behind it or is it just historical?

Also in C if char and 'a' have different size, does that mean that when we write char ch = 'a';, we are doing implicit type-conversion?

Community
  • 1
  • 1
Pratt
  • 851
  • 2
  • 10
  • 16
  • 1
    possible duplicate of [Size of character ('a') in C/C++](http://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c) – Bo Persson May 15 '12 at 20:16
  • 1
    I'm actually surprised by `sizeof("a") : 2` - all these years, and I always assumed that would be the same as `sizeof(char*)` - I might have actually used it if I had known otherwise. –  May 22 '12 at 19:28

2 Answers2

61

In C, character constants such as 'a' have type int, in C++ it's char.

Regarding the last question, yes,

char ch = 'a';

causes an implicit conversion of the int to char.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • +1: @Pratt: in 1970s C usage, `int` was the return type for `getc()`. It uses -1 to indicate EOF which was also a handy convention for many other things frequently done with `char` expressions, like indexing into an array for character-break processing, char string transformation (like `stricmp()`) etc. – wallyk May 15 '12 at 18:43
  • 3
    @wallyk: `getc()` still returns `int`, and it still returns `EOF` to indicate that there are no more characters to be read. `EOF` is defined to be a negative `int` value; it's typically `-1`. – Keith Thompson May 15 '12 at 18:54
  • 3
    And the reason C++ changed the rules is because you can overload functions. – Jesse Good May 15 '12 at 21:18
  • Very nice topic,answer and comments. :) – The Mask May 15 '12 at 23:03
  • @KeithThompson the `int` returned by `getc` and friends is a different sort of `int` to the character constants though! Character constants may be negative, but the `getc` are all non-negative values. What would be negative character values are adjusted modulo `UCHAR_MAX+1`. – M.M Sep 26 '16 at 09:09
  • @M.M: I'm not sure what you mean by "a different sort of `int`". Yes, character constants can be negative (but only if plain `char` is signed, and only for values outside the basic execution character set). And of course `getc()` can return `EOF`, which is negative. – Keith Thompson Sep 26 '16 at 15:12
1

because there is no char just intgers linked int a character

like a is 62 i guess

if you try printf("%c",62); you will see a character

ucefkh
  • 2,509
  • 2
  • 22
  • 18