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(“%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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
100.3000005
i = 10Originally 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 */;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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)