+ 1

output??

int main() { char a[] = "world"; printf("%d %d ",strlen(a),sizeof(a)); return 0; }

29th Dec 2016, 5:47 PM
Somnath Ghosh
Somnath Ghosh - avatar
4 Respostas
+ 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.
29th Dec 2016, 5:59 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
31st Dec 2016, 5:19 AM
Hatsy Rei
Hatsy Rei - avatar
0
how 6byte??
31st Dec 2016, 5:12 AM
Somnath Ghosh
Somnath Ghosh - avatar
0
does it always declare 1 byte extra
2nd Jan 2017, 2:13 PM
Somnath Ghosh
Somnath Ghosh - avatar