C Programming - Library Functions - Discussion

Discussion Forum : Library Functions - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>

int main()
{
    int i;
    i = scanf("%d %d", &i, &i);
    printf("%d\n", i);
    return 0;
}
1
2
Garbage value
Error: cannot assign scanf to variable
Answer: Option
Explanation:
scanf() returns the number of variables to which you are provding the input.

i = scanf("%d %d", &i, &i); Here Scanf() returns 2. So i = 2.

printf("%d\n", i); Here it prints 2.

Discussion:
9 comments Page 1 of 1.

Raman said:   7 years ago
Can anybody explain in detail?

Amit Shrivastav said:   9 years ago
The output is -1, let me correct or explain how I got output a is -1.

Arungv said:   1 decade ago
#include<stdio.h>

int main()

{
int i;
i = scanf("%d %d", &i, &i);
printf("%d\n", i);
return 0;
}

Output is -1(this only i got ) in 32 Linux platform.

Bilal said:   1 decade ago
When you write scanf() it awaits for an input, why would you get get 2 as output when the input is not even entered?

Maneesh Singh said:   1 decade ago
scanf is also a function and it has a return type so whenever you call any scanf function in printf it gives you return value either 1 or 0. 1 means success and 0 means fail. for ex -

int a=10, b= 20;
printf("%d", add(a, b));

int add(int x, int y)
{
return (x+y);
}

In this ex printf will print return value of add fn that is 30..same way scanf will print its return value if success then 1 else 0...

Try this:

int d =0;
printf("d= %d, d1 = %d, d2 = %d\n", d, scanf("%d", &d), d);

Sachin said:   1 decade ago
How this works?

Akanksha Jain said:   1 decade ago
Could you please explain how its throwing output as 2, when I ran this in gcc compiler(32 bit) which is provided here, it was throwing output as "-1"?

Vasantha deepika said:   1 decade ago
Can any explain in detail how does it works?

Ravi said:   1 decade ago
How this works?

Post your comments here:

Your comments will be displayed after verification.