There are lots of errors, here.
"sizeof(a)
gives me output: 11
(length of the string)"
The length of the string is 10
, not 11
. sizeof(a)
gives you the length of the array.
"why is it so, why isn't the output sizeof(a)
=4
since when I try to print a
it gives me an address value"
Here are two methods of "printing a
" which do not "give you an address value":
puts(a);
and:
printf("%s\n", a);
so your logic is flawed, and this is the source of your confusion. "Printing a
" only "gives you an address value" when you explicitly or implicitly elect to do so.
sizeof(a)
gives 11 in this case because the C language defines the sizeof
operator to give you the size of an array when an array is the operand. This, I'd argue, is the most natural behavior people would expect, so presumably that's why it is defined as such.
"and hence an integer."
In any case, an address is an address, not an integer. At best you could argue that it ought to give you the size of a pointer, but certainly not the size of an integer.