0

When I am printing sizeof('b') in C it is printing 4, but it should print 1 because 'b' is not a integer, it is a char.

 #include<stdio.h>
 int main(){
     printf("the size of b is %d",sizeof('b')); 
 }
StoryTeller - Unslander Monica
  • 159,632
  • 21
  • 358
  • 434
  • 1
    Please have a look at this question: [Size of character ('a') in C/C++](https://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c) 'b' is actually converted to an integer. – Maarten Arits Sep 04 '18 at 12:26
  • 1
    There is type promotion and alignment in C. The sizes are not so black and white. I believe all chars are promoted to ints – Rafael Sep 04 '18 at 12:28
  • 5
    There's no conversion or promotion when dealing with sizeof. `'b'` is a plain `int` in C. Not a `char`. – StoryTeller - Unslander Monica Sep 04 '18 at 12:31
  • 1
    Do study the duplicate. `'b'` is an `int` in C, **not** a `char`. – Bathsheba Sep 04 '18 at 12:32
  • 1
    `printf("the size of b is %d",sizeof('b'));` invokes undefined behavior. `sizeof()` returns `size_t`, not `int`. [The proper format length modifier for `size_t` is `z`](https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p7). – Andrew Henle Sep 04 '18 at 12:59
  • This is common misunderstanding of the integer constants. They can be entered decimal, hex, (not standard C) binary, octal and as a integer code of the character. And it is your case. – 0___________ Sep 04 '18 at 13:44

0 Answers0