+ 1
output??
int main() { char a[] = "world"; printf("%d %d ",strlen(a),sizeof(a)); return 0; }
4 Answers
+ 6
The output will be:
5 6
strlen(a) returns the length of the string stored in char array a.
sizeof(a) returns the size in bytes of the object representation.
Now the question here is, why does sizeof(a) always return an integer larger than strlen(a) by 1.
We know that sizeof(char) always return 1.
This means that the memory allocated for char a[] to store "world" is actually 6 bytes, not 5 bytes. The program will automatically allocate memory in such a manner that the allocated bytes of memory will always be one byte larger than the string length.
+ 6
The amount of memory used to store 1 char is 1 byte. The amount of memory used to store char array of size 5 is 5 bytes. However, the size of the array was not specified, hence the system allocates 6 bytes of memory to store "world" in order to avoid exceptions. This is why sizeof() returns 6.
0
how 6byte??
0
does it always declare 1 byte extra