2

Possible Duplicate:
Why are C character literals ints instead of chars?
why sizeof('a') is 4 in C?

#include<stdio.h>
int main()
{
  char ch;
  fflush(stdin);
  ch=getchar();
  printf("ch= %d a=%d char=%d", sizeof(ch),sizeof('a'),sizeof(char));
}

I type in 'a' (without quotes) as input , and the output I got in my gcc version 4.5.1 is :

ch= 1 a=4 char=1

My question is :

If sizeof(c) is 1 , then how can sizeof('a') be 4 ?

Community
  • 1
  • 1
sbose
  • 1,791
  • 5
  • 24
  • 46
  • This difference between C and C++ is really only marginal. In both languages expressions of type `char` are automatically promoted to `int` in most contexts. As a result in operations such as `'0' + 5` the computation is done with `int` anyhow. – Jens Gustedt Jan 02 '12 at 09:16
  • @Jens: For once you're wrong. Promotions don't come into play at all with `sizeof`. It's purely a matter of the actual type of character literals, which **is** a big difference between C and C++. – R.. GitHub STOP HELPING ICE Jan 02 '12 at 15:43
  • @R.. this is basically what I meant, therefore I said "most". Perhaps I missed to express my pov clearly. The only contexts where this distinction is relevant are `sizeof` and perhaps assignment. In all *other* contexts `char` promotes (basically) to `int`. Whether or not this is a big difference or not is really a question of appreciation. – Jens Gustedt Jan 02 '12 at 15:54

3 Answers3

10

In C, a literal character (e.g., 'a'), is an int, not a char. In C++, however, literal characters are actual chars.

Daniel Gallagher
  • 6,915
  • 25
  • 31
  • Okay, thanks. Could you point me to some compiler docs for these? – sbose Jan 02 '12 at 07:26
  • 1
    For C, the C99 standard 6.4.4.4/10 "Character constants" says, "An integer character constant has type `int`". C++03 says in 2.13.2 "Character literals", "An ordinary character literal that contains a single c-char has type `char`". – Michael Burr Jan 02 '12 at 07:35
  • Wow, that's useful @MichaelBurr! – sbose Jan 02 '12 at 07:40
  • 1
    Also, for historic interest, Stroustrup says in "Design and Evolution of C++" that, "In C, the type of a character literal such as `'a'` is `int`. Surprisingly, giving `'a'` type `char` in C++ doesn't cause compatibility problems. Except for the pathological example `sizeof('a')`, every construct that can be expressed in both C and C++ gives the same result". – Michael Burr Jan 02 '12 at 07:44
2

Because in C character constants, such as 'a' have the type int.

There's a C FAQ about this suject:

Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs).

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

On most of the systems 'a'== 97, which ASCII value of the character 'a'.

for e.g:

int i = 97;
char c = i;

If you print the value of variable c with the option '%c' you'll see 'a' on the screen. for your question we can deduce sizeof('a') as sizeof(i) equable to sizeof(97);

You should be able to find this in C standards document.

dicaprio
  • 713
  • 2
  • 8
  • 25