edited by
875 views
6 votes
6 votes

What is the output of the code?  

#include <stdio.h >
int main()
{
int a;
printf("%d",scanf("%d",&a));
return 0;
}
  1. $10$
  2. $9$
  3. $-1$
  4. An undefined behavior
edited by

4 Answers

Best answer
6 votes
6 votes
scanf return three type of value 0, 1, -1

 

Example



#include <stdio.h>
int main()
{
int i ;

int j=scanf("%d",&i);

printf("%d",j);
}

1 if input enter is integer

-1 if no input enter

0 if input enter is invalid  // for above code if we  enter input a or b ,aajjs etc not integer 

so best answer is option c if no input from keyboard

D is wrong as  we can see output can define by above statement of scanf

selected by
2 votes
2 votes
i think option c should be 1 not -1...bcoz it run successfully and printf return number of successfully input..may be i wrong
2 votes
2 votes

The printf() function

The printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value.

The scanf() function

The scanf() function is used for obtaining the input from the user. It returns the number of input values that are scanned. If there is some input failure or error then it returns EOF (end-of-file).

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
199 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”...