I asked this in the other forum, but this one might be better.
The thing is that I have problems with scanf. Ok, everybody tells me not to use it since it eats a lot of memory. I know, was just playing around....
The problem is best described by showing the following code. Please note the commented block.
int main (void) { char test[16]; int k, res; ioinit(); //Setup IO pins and defaults while(1) { /* printf("Waiting for string input:\n"); res = scanf("%s", test); printf("res %d \n %s\n", res, test); */ printf("Waiting for int input:\n"); res = scanf("%3d", &k); printf("res %d \n %d\n", res, k); } return(0); // never get here }
the output is as follows with only the int reading when I input once a non-int character (e.g. a 'y'). It will never stop and run continuesly :
res 0 0 Waiting for int input: res 0 0 Waiting for int input: res 0 0 Waiting for int input: res 0
That's not good.
Now if I include the string input routine it works correct:
Waiting for string input: res 1 test1 Waiting for int input: res 1 123 Waiting for string input: res 1 test2 Waiting for int input: res 1 321 Waiting for string input:
and now if I put a 'y' at the prompt for an int:
Waiting for string input: res 1 hallo Waiting for int input: res 1 0 Waiting for string input: res 1 ahallo2 Waiting for int input: res 1 222 Waiting for string input:
Apparantly res=1 even when a rubbisch character for the int is put in...
Questions:
- Am I doing the routines correctly?
- Is the scanf routine buggy?
Ps. I use the latest winavr / avr studio
Thanks