Newlib printf/scanf problems.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
rspencer
Posts: 18
Joined: Wed Feb 03, 2010 12:05 am

Newlib printf/scanf problems.

Post 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?
rspencer
Posts: 18
Joined: Wed Feb 03, 2010 12:05 am

Re: Newlib printf/scanf problems.

Post by rspencer »

Ok, fixed the reading of integer thing. Turns out I forgot to return the newline with the whole string.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Newlib printf/scanf problems.

Post 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.
rspencer
Posts: 18
Joined: Wed Feb 03, 2010 12:05 am

Re: Newlib printf/scanf problems.

Post 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.
Post Reply