Page 1 of 1

Newlib printf/scanf problems.

Posted: Fri Dec 07, 2012 11:39 am
by rspencer
I recently got newlib working for my OS and compiled a simple program with a cross compiler:

Code: Select all

int main()
{
  printf("Hello world (input a number): ");
  int a = 7;
  scanf("%d",&a);
  printf("%d",a);
  return 0;
}
Now this prints out the message and then reads from file descriptor 0. This seems ok as 0 is stdin. (The output is a write to fd 1).
However, no matter what number I enter it just reads 1024 bytes from stdin again. Is this expected behaviour???
Moreover, and more worrying, when the scanf is excluded, no write syscall occurs. Ie a program that only printfs has no effect. BUT a program that prints to stderr (fprintf(stderr,"Hello")) makes the syscall.
Afaik, the only difference between stdio and stderr wrt an application is that the one is buffered and the other not. But surely both must get written at some time. Am I missing something?

Re: Newlib printf/scanf problems.

Posted: Fri Dec 07, 2012 11:48 am
by rspencer
Ok, fixed the reading of integer thing. Turns out I forgot to return the newline with the whole string.

Re: Newlib printf/scanf problems.

Posted: Fri Dec 07, 2012 12:46 pm
by Owen
The C standard defines stdout to be line buffered; i.e. it will flush its internal buffer whenever a newline character is encountered

(stderr is unbuffered and will flush immediately)

These are basic C standard things you should know.

Re: Newlib printf/scanf problems.

Posted: Sat Dec 08, 2012 6:08 am
by rspencer
Huh. Would you look at that. Thanks! I have been using c-type languages for ages and never came across that. I suppost that is what comes of using c++ over c.