523 views

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

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.
@srestha

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

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

### 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.

You can not say the answer is -1.
i think option c should be 1 not -1...bcoz it run successfully and printf return number of successfully input..may be i wrong

## 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).

1
201 views