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.

Ritu said:   2 decades ago
Why it returns one?

Sundar said:   2 decades 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)

Mani said:   1 decade ago
Thank you.

Rakesh said:   1 decade ago
I'm still confused. As 25 is given as input. Why it returns 1? explain.

Amarjit said:   1 decade ago
Excellent answer.

Satyam said:   1 decade ago
If we don't write the statement return than what will be the output?

Rockstar said:   1 decade ago
Thanks ritu it really hlped.

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.

Amit said:   1 decade ago
Thanks Sundar.............

Randy said:   1 decade ago
If you compile the code. Why the result is -1? :).


Post your comments here:

Your comments will be displayed after verification.