1

I have the following code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[]){
    printf("size of tab = %d\n", sizeof('\t'));
    printf("size of a = %d\n", sizeof('a'));
    printf("size of char = %d\n", sizeof(char));
}

Output:

size of tab = 4
size of a = 4
size of char = 1

Why is the size of 'a' and the size of char different. Isn't 'a' a char?

James Mitchell
  • 2,387
  • 4
  • 29
  • 59
  • 1
    `sizeof('\t')` is actually calculating the size of an `int` where `int` is the ASCII value of `'\t'` and similar is the case for `sizeof('a')` – Md Johirul Islam Sep 25 '17 at 02:07
  • `'a'` is an `int`. All constants in C are at least `int`. – chux - Reinstate Monica Sep 25 '17 at 02:17
  • 6.4.4.4 Character constants p2 _An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'._ and p10 _An integer character constant has type int._ 6.5.3.4 The sizeof ... operators p4 _When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1._ – BLUEPIXY Sep 25 '17 at 02:34

1 Answers1

1

'a is the int containing the character a. take a look at the values of these expressions:

sizeof((char) 'a');
char a = 'a';
sizeof(a);
Sam Hartman
  • 6,210
  • 3
  • 23
  • 40