scanf semihosting problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

scanf semihosting problem

1,086 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by navin on Wed Jan 30 23:54:36 MST 2013
Hi,

I am trying to input some value form the IDE using the semihosting (via scanf) but the scanf returns with a random number even before I type in a number. Given the fact that printf is working, I think the semihosting has no issues here.
The code that I used.
void Test(void)
{
  int ch;
  printf ("Enter a number \n");
  scanf ("%d", &ch);
  printf ("%d", ch);
}
Running this code results in following output:
Enter a number
268436316
The scanf() appears to be non blocking. it does not wait for any input.
Also, getchar always returns -1.

Thanks,
Navin
0 Kudos
Reply
1 Reply

785 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Jan 31 09:56:22 MST 2013
The input here is buffered. Thus the most likely reason for your issue with scanf is that you have already been typing on your PC's keyboard before the scanf was executed.

Simplest way to avoid such problems is add the following line immediately before the scanf:

fflush(stdin);


With regards to getchar, this seems to work for me. But remember, again, because of the buffering implicit in semihosting, you will need to hit <ENTER> on the PC keyboard after each character.

Slight aside - I recommend you terminating your printf() calls with a newline character - to force a buffer flush.

Regards,
CodeRedSupport
0 Kudos
Reply