C | String | Question 2

What is the output of following program?

# include <stdio.h>
  
int main()
{
   char str1[] = "GeeksQuiz";
   char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'};
   int n1 = sizeof(str1)/sizeof(str1[0]);
   int n2 = sizeof(str2)/sizeof(str2[0]);
   printf("n1 = %d, n2 = %d", n1, n2);
   return 0;
}

(A) n1 = 10, n2 = 9

(B) n1 = 10, n2 = 10

(C) n1 = 9, n2 = 9

(D) n1 = 9, n2 = 10


Answer: (A)

Explanation: The size of str1 is 10 and size of str2 9.

When an array is initialized with string in double quotes, compiler adds a ‘\0’ at the end.



My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :



Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.