edited by
690 views
4 votes
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
edited by

3 Answers

Best answer
6 votes
6 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 votes
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 votes
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:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
392 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
351 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
198 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 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
202 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”...