I wrote this test program to get the return value of the scanf() function.
however in the test, if i input a integer, the program is normal, but when i input a string, it will go into a endless loop. who can explain the reason for that?
another thing is that is the escape character \0 stand for the ascii code 0? when I print a “\12”, i will get a new line, but when I print “\9”, I couldn’t get a “TAB”(the TAB’s ascii code is 9).
#include <stdio.h>
int main(void){
int x;
int temp;
//printf("\13");
for(;;){
temp=scanf("%d",&x);
printf("%i",temp);
}
}
You’re telling
scanfto read an integer. If you input a string thescanfwill wait for an integer input.scanfalso does not purge the input so it will read the same thing over and over again. You should check the value oftempwhen it successfully read something it will return the count of successful reads. In your case on the string input it should return 0.Also depending on where you output and the shell you use the tab output may or may not be ignored or interpreted differently.