Why the size of s2 array is 8 ???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why the size of s2 array is 8 ????

int x, y; char s1[] = "Sololearn"; char s2[] = {'S', 'o', 'l', 'o', 'q', 'u', 'i', 'z'}; x =sizeof(s1); y = sizeof(s2); printf("%d, %d",x, y); O/P :- 10,8

20th Jul 2019, 11:32 AM
Suraj Patra
Suraj Patra - avatar
7 Answers
+ 1
if you store the array inside curly braces the contents of the array will be the ones you say it to be
20th Jul 2019, 3:45 PM
Bebida Roja
Bebida Roja - avatar
+ 2
Actual strings have an enclosing NULL character (\0), so that's why the first one it's bigger. The second is just an array of characters
20th Jul 2019, 11:37 AM
Airree
Airree - avatar
+ 2
because it contains 8 characters
20th Jul 2019, 11:37 AM
Bebida Roja
Bebida Roja - avatar
+ 2
Bebida Roja, so when we store string in array during declaration time with every character in single quote the string does not contain any null character? Please just confirm this, I will be happy then .
20th Jul 2019, 3:38 PM
Suraj Patra
Suraj Patra - avatar
+ 1
Airee you mean the second one has no null character in last ??
20th Jul 2019, 11:56 AM
Suraj Patra
Suraj Patra - avatar
+ 1
yes, any string within double quotes will contain a null terminating character, thats why the first one has size 10, but your question was about the second string.
20th Jul 2019, 1:50 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Babida Roja, Thanks man
20th Jul 2019, 7:56 PM
Suraj Patra
Suraj Patra - avatar