1

The program below produces

output - 1 4 4

#include<stdio.h>
void main()
{
    char ch;
    ch='A'; 
    printf("%d %d %d\n",sizeof(ch),sizeof('A'),sizeof(3.2f));

}

Why is the size of character constant is 4 bytes ?

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
Anjaneyulu
  • 434
  • 5
  • 21

2 Answers2

4

Because according to the C standard the type of a character constant is int, and not char.

So in effect, this is the sizeof(int) on your platform.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

ch is char type so 1 byte.

'A' is int type so 4 bytes. Because in C the character constant is an int type.

Last is float value so 4 bytes.

These values according to the machine you are using.

Edit -

The range of int and float depends on the machine you are using, 16 bit int is as common as 32 bit int.

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21