Character Array MCQ 8 : String Variable Length inside Printf Statement



Character Array MCQ 8 : String Variable Length inside Printf Statement


What is the Output of the Following Program ?

#include<stdio.h>
#include<string.h>
void main()
{
char str[]="\0";
if(printf("%d",strlen(str)))
   printf("Non Empty String");
else
   printf("Empty String");
}

Options :

  1. 1Non Empty String
  2. 0Non Empty String
  3. 1Empty String
  4. 0Empty String

Output:


Switch to String MCQ Home : Click Here


How and Why ?

  • While Considering String Length ‘\0’ is Ignored. [i.e str = “\0” has Length = 0 ]
  • Printf Returns Integer Value [ Number of Characters Printed On Screen ]
  • String Length is Zero , So ‘0’ is printed on Screen.
  • As Only 1 Character is Printed on Screen so Printf Returns 1.
  • if(1) is True Condition and If Part is Executed