197k views
1 vote
Suppose that we call scanf as follows: int count = scanf("%d %f %d", &i, &x, &j); (i, j are int vaiables and x is a float varibale). Assume that the input stream contains the characters are 0.1 0.2 0.3, give the values of i, x, and j after the call.

User Yosmar
by
8.5k points

1 Answer

5 votes

Final answer:

When using scanf with incorrect format specifiers, as in the case of reading floats with %d, scanf fails to read and assign values, leaving variables unchanged and returning a count of 0.

Step-by-step explanation:

Understanding scanf in C Programming

When scanf is called as int count = scanf("%d %f %d", &i, &x, &j); with i and j being integer variables and x being a float variable, the function attempts to read input in the specified format. Since the input stream contains the characters 0.1 0.2 0.3, the first scanf conversion specification %d expects an integer, but it encounters a floating-point number 0.1, which it cannot process. Thus, the scanf function fails to read an integer for i, and the process stops. As a result, the values of i, x, and j are left unchanged, and count will be 0, indicating that no assignments were made.

It's important to match the format specifiers in scanf with the type of input data. Otherwise, scanf will not perform as expected, leading to potential errors in data readings or undefined behavior due to mismatched input.

User Zgabievi
by
7.7k points