• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple ScanF() doesn't work in loop

The Pentium Guy

Diamond Member
Hey, long time since I've browsed these forums

I have a microcontroller which I'm trying to talk to through SecureCRT on a COM port.

I have a simple loop:

char c;

//Set up UART ports...etc
while (1)
{

printf ("scanning...\n");
scanf("%c", &c);
printf ("scanned char: %c\n",c);
}
It types in "scanning..." and waits for me to type in a character, but it must be followed by a line feed (\n). Once it scans it outputs the scanned character... as planned

But on the next iteration of the loop it assumes that the same character was scanned and it outputs it again and again. It essentially skips over the scanf line. Typing a new character won't work either

Output:scanning....
Input: 1\n
Output:scannedchar: 1
scanning....
1 scanned char: 1\n
scanning.....
1 scanned char: 1

Anyone know what the deal is?
 
As I recall scanf does not flush the newline character at the end of the input string. Therefore subsequent calls to scanf will read that newline. You can use getchar() to flush that newline and then call scanf again.
 
Back
Top