Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

Correct answer: n1 = 10, n2 = 9

str1 is initialised from a string literal, so the compiler adds a terminating null: 9 letters plus '\0' is 10 bytes. str2 is initialised from an explicit list of nine characters, with no null added, so it is 9 bytes - and, lacking a terminator, it is an array of characters rather than a usable C string.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.