#include <stdio.h>
#include <string.h>
main()
{
printf("%d \n ",sizeof(' '));
printf("%d ",sizeof(""));
}
output:
4
1
Why o/p is coming 4 for 1st printf and moreover if i am giving it as '' it is showing error as error: empty character constant but for double quote blank i.e. without any space is fine no error?
The ' '
is example of integer character constant, which has type int
(it's not converted, it has such type). Second is ""
character literal, which contains only one character i.e. null character and since sizeof(char)
is guaranteed to be 1
, the size of whole array is 1
as well.
' '
is converted to an integer character constant(hence 4 bytes on your machine), "" is empty character array, which is still 1 byte('\0') terminated.
Here in below check the difference
#include<stdio.h>
int main()
{
char a= 'b';
printf("%d %d %d", sizeof(a),sizeof('b'), sizeof("a"));
return 0;
}
here a is defined as character
whose data type size is 1 byte
.
But 'b' is character constant
. A character constant is an integer
,The value of a character constant is the numeric value of the character in the machine's character set. sizeof char constant is nothing but int
which is 4 byte
this is string literals "a" ---> array character whose size is number of character + \0 (NULL)
. Here its 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With