So I've got the following code:
int q, x, y;
char l;
for (q = 0; q<10; q+=1) {
printf("%d\n", q);
if (3 == scanf("%c %d %d", &l, &x, &y)) {
printf("%d\n", q);
}
}
I would expect this to run "scanf" 10 times. Each time it will print 'q' before I enter anything, and once again after. So the expected output should be this (correct me if I'm wrong):
0
> a 1 1
0
1
> b 2 2
1
2
> c 3 3
2
...and so on.
But I actually get this.
0
> a 1 1
1
1
2
> a 2 2
3
3
4
> a 3 3
5
5
6
> a 4 4
7
7
8
> a 5 5
9
9
If I take out the scanf function, it counts from 0-9 as expected. What's happening with the scanf function to make the for loop act so weirdly? I've got some experience in Java/Python, but I'm totally new to C.
Editing the code based on your comments to this (apologies for weird formatting):
for (q = 0; q<10; q+=1) {
printf("%d: ", q);
if (3 == scanf("%c %d %d ", &c, &x, &y)) printf("%d\n", q);
else { printf("WTF %d\n", q); }
}
half solves the problem. It works for all of the iterations EXCEPT for the first one. As so:
0: a 0 0
WTF 0
1: b 1 1
1
2: c 2 2
2
What's going on there? There is other outputted code above that first line, from another part of the program. Could that be causing this?