I have a problem with scanf()
and printf()
function in a snippet of code like this:
#include <stdio.h>
int main() {
int a;
int b;
int c;
scanf("%d %d", &a, &b);
while (c >= 2) {
c = a % b;
a = b;
b = c;
printf ("%d\n", c);
}
return 0;
}
What I expect to happen, and happens in my brother's Code::Block, is for the program to wait for input from stdin
and then print to stdout
the results, one per line, until it reaches the highest common divisor.
However, when I type it in vi
and then compile it with gcc
and run the program from my terminal, the program correctly takes the input but exit without returning anything to stdout
.
If I comment out the scanf()
line and hardcode any number to a
and b
variables, everything works as expected.
I'm trying to learn C and I've read basic documentation on the functions, but I can't help to understand this kind of behaviour.
I've tried to put a setbuf(stdout, NULL)
before declaring variables but nothing changed.
Can somebody give me a clue?