• retagged by
1,771 views
4 4 votes

Read this code snippet :

void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf(" GATE 2018\n");
else
printf("Forget GATE\n");
}

The output is :

  1. Compiler Error
  2. Forget GATE
  3. GATE $2018$
  4. Runtime error

3 Answers

Best answer
7 7 votes
Printf returns number of characters printed.
Printing NULL will return 0.
But there is a new line after NULL.   (\n)
Because of that new line, 1 will be returned by printf.
So output is C.
• selected by
2 2 votes
Explanation:
Printf will return how many characters does it print.

Hence printing a new line character "\n" returns 1 which makes the if statement true, thus "GATE 2018" is printed.
• edited by
0 0 votes
void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf(" GATE 2018\n");
else
printf("Forget GATE\n");
}

Because of \n inside printf condition will become true (\n is newline character so it will print nothing but will insert a new line) and output will be GATE 2018.

void main()
{
int i;
char a[]="\0";
if(printf("%s",a))
printf(" GATE 2018\n");
else
printf("Forget GATE\n");
}

Now output will be : Forget GATE

Some ref : https://stackoverflow.com/questions/18654465/how-0-is-treated-in-printf#:~:text=So%20empty%20is%20printed%20as%20output.&text=The%20main%20concept%20here%20is,in%20the%20statement%20if%20any%20

Answer:
Position:
Show:

Related questions

0 0 votes
1 1 answer
594
594 views
Bikram asked May 14, 2017
594 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 0 votes
1 1 answer
555
555 views
Bikram asked May 14, 2017
555 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”, y)...
3 3 votes
1 answers 1 answer
1.7k
1.7k views
Bikram asked May 14, 2017
1,694 views
What is the output of the below mentioned code snippet?#include <stdio.h int main() { int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater"); ...
2 2 votes
1 answers 1 answer
947
947 views
Bikram asked May 14, 2017
947 views
Read the following program fragment: #include<stdio.h int main() { int p = 2, q = 5; p = p^q; q = q^p; printf("%d%d",p,q); return 0; }The output is:$5 \ 2$$2 \ 5$$7...