4

Possible Duplicate:
Why are C character literals ints instead of chars?

http://ideone.com/lHYY8

int main(void)
{
        printf("%d %d\n", sizeof('c'), sizeof(char));
        return 0;
}

Why does sizeof('c') return 4 instead of 1?

Community
  • 1
  • 1
ob_dev
  • 2,808
  • 1
  • 20
  • 26

4 Answers4

16

Because in C character constants have the type int, not char. So sizeof('c') == sizeof(int). Refer to this C FAQ

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

cnicutar
  • 178,505
  • 25
  • 365
  • 392
4

One (possibly even more extreme) oddity that also somehow justifies this, is the fact that character literals are not limited to being single character.

Try this:

printf("%d\n", 'xy');

This is sometimes useful when dealing with e.g. binary file formats that use 32-bit "chunk" identifiers, such as PNG. You can do things like this:

const int chunk = read_chunk_from_file(...);
if(chunk == 'IHDR')
  process_image_header(...);

There might be portability issues with code like this though, of course the above snippet assumes that read_chunk_from_file() magically does the right thing to transform the big-endian 32-bit value found in the PNG file into something that matches the value of the corresponding multi-character character literal.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.

A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.

So sizeof('a') is equivalent to sizeof(int)

And this question is a duplicate of why sizeof('a') is 4 in C?

Community
  • 1
  • 1
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • 3
    This K&R sentence has nothing to do with the fact that a character constant is of type `int` in C. `char`, `short` or even `_Bool` are also integers. – ouah Jan 02 '12 at 15:41
  • @ouah Could you please elaborate your first statement? w.r.t second statement, you can add `enum` to the list! – Sangeeth Saravanaraj Jan 02 '12 at 16:09
1

cnicutar is completely right of course. I just wanted to add the reason for this. If you look at functions line fgetc, you'll notice that it also returns an int. It's because a char can represent any character from 0x00 to 0xFF, but an additional value is needed in order to represent EOF. So functions that return a character from input or a file often return an int, which can be compared with EOF, which is usually defined to be -1, but it can be anything that isn't a valid character.

Dan
  • 10,531
  • 2
  • 36
  • 55
  • 1
    Actually `EOF` is required to be negative and thus disjoint from any possible value of *unsigned* char converted to `int` unless the width of `int` is the same as `CHAR_BIT`. – R.. GitHub STOP HELPING ICE Jan 02 '12 at 15:54