C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 13)
13.
What will be the output of the program if value 25 given to scanf()?
#include<stdio.h>

int main()
{
    int i;
    printf("%d\n", scanf("%d", &i));
    return 0;
}
25
2
1
5
Answer: Option
Explanation:

The scanf function returns the number of input is given.

printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one).

Therefore, the output of the program is '1'.

Discussion:
23 comments Page 1 of 3.

Sundar said:   1 decade ago
@Ritu

The scanf() function returns the count of number of input fields successfully scanned, converted, and stored.

Therefore, scanf("%d", &i) will return 1 because it reads one input from the user.

If we use a scanf() statement like scanf("%d %d", &intRollNo, &intAge), it will return 2, because it reads two input values.

Hope you understand this. Have a nice day!
(1)

CHALLA SANTHOSH said:   9 months ago
The scanf keyword is used inside the printf.
So the scanf will do that it will take inputs but it will return how many inputs are taken.
So if there is only one integer it will return one if there is two inputs for the scanf it will return two.
(1)

Akshata Vibhute said:   1 decade ago
I agree with your answer @Sundar but as we all know scanf reads the given input. And here its being written with printf statement.

So not able to know how it will come to know that it must read the number of times its read?

Rakesh said:   1 decade ago
As scanf returns total number of input is given to it.

Try again it will return 1, as number of input is 1. Try also this,

int i;
i = printf ("rakesh") ;
printf("%d", i);

Output will be "rakesh6".

Rubini said:   6 years ago
What will be the output of the following statement?

print("%X%x%ci%x",11,10,'s',12); Please explain with answer.

Ranjana said:   1 decade ago
The scanf() function returns that how many inputs are given by the as we see here one input is given so answer is 1.

Sowmiyalaxmi said:   9 years ago
Though scanf may get total number of inputs, here the question is &I then how the output will be one. Explain.

Pranali said:   8 years ago
If scanf() returns number of inputs(I agree) but what about printf()?(what it will return)

Shaikh Sahil said:   3 years ago
If scanf is given 25 then why it is returning 1?

Can anyone please explain me?

Vijay said:   9 years ago
But in GCC it is different.

It returns, how bytes are read?

Example int is 4.


Post your comments here:

Your comments will be displayed after verification.