Consider the following code:
#include <stdio.h>
int main() {
float x;
int i;
char c;
printf("type three inputs:\n");
scanf("%3d %5f %c", &i, &x, &c);
printf("i is %d, x is %f and c is %c", i, x, c);
return 0;
}
input: 454 3567.7 s
which prints to the screen:
i is 454, x is 3567.000000, c is 7
My question is exactly how does the computer interpret this input?
I think it is something like the following:
%3d => first 3 characters are taken in as i, i.e. 454
%5f => the next 5 characters are taken as x, i.e. 3567. (Note: I have included the decimal)
%c => takes the next character only as c, i.e. 7
s is disregarded
My theory also is that spaces do not affect the input
i.e. if I were to input: 454 3567.7 s
the same output would result, however, if I were to input the following:
input: 45 4 3567.7 s
no output would result as 45 4 cannot be successfully converted by %3f