Suppose that we call scanf as follows:
scanf(“%d%f%d”, &i, &x, &j);
If the user enters
10.3 5 6
what will be the values of i, x and j after the call? (Assume that i and j are int variables and x
is a float variable).
Printable View
Suppose that we call scanf as follows:
scanf(“%d%f%d”, &i, &x, &j);
If the user enters
10.3 5 6
what will be the values of i, x and j after the call? (Assume that i and j are int variables and x
is a float variable).
Suppose that we call scanf as follows:
scanf(“%f%d%f”, &x, &i, &y);
If the user enters
12.3 45.6 789
what will be the values of x, i and y
So perhaps you should create a small C program and try each one and see the results.
Well i tried but i am not sure if my results are correct..... I have been told these questions are tricky so please someone can help me out to see hwta the final and correct results will be.THANKS
> Well i tried but i am not sure if my results are correct
So what ARE your results?
> ..... I have been told these questions are tricky
They're not tricky at all.
100.3000005
i = 10Quote:
Originally Posted by vicky2
x and j have indeterminate values and cannot be used
Also, the return value from that scanf() call was 1 to let you know only 1 assignment was made.
x = 12.3
i = 45
y is indeterminate and cannot be used.
Also, the return value from that scanf() call was 2 to let you know only 2 assignments were made.
~~~~~~~~
Remember to always test the return value of scanf()
Code:if (scanf("%f%d%d", &x, &i, &j) != 3) /* error */;
Oops ... my bad at post #7
first scanf gets 10, 0.3, and 5 (and keeps " 6" in the buffer)
second scanf gets 12.3, 45, and 0.6 (and keeps " 789" in the buffer)