in Programming in C edited by
951 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
in Programming in C edited by
by
951 views

4 Comments

i guess the program will be waiting for the input and wont terminate ever.. I just get stuck hoping to get a input. Hence the printf() will never do any output.

Hence can predict the output. hence D.
0
0
@srestha

what will the correct choice
0
0
It showing the output, compliler terminated
0
0

4 Answers

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

1 comment

You can't say that the answer is -1 

If the above question is like this:

#include <stdio.h>

int main()

{

int i ;

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

printf("%d",j);

// No input is entered to the program.

}

Then the answer should be -1.

2
2
2 votes
2 votes
You can not say the answer is -1.
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