Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf() and scanf()

Status
Not open for further replies.

thelearners

Programmer
Jul 1, 2004
8
0
0
US
Hi,

I'm trying to learn C and coding it on AS400. Here is my
program.

#include <stdio.h>

int main(void)
{
int month;
printf("enter month: ");
scanf("%d", &month);
return 0;
}

I compiled it with command CRTBNDC. When I called it, I got a blank screen with a command line. When I entered some number ie 3 then the screen printed out as below.

> 3
enter month:
Press ENTER to end terminal session.

It doesn't seem right to me. I thought right after I call the program, it suppose to prompt on the screen say "enter month". Can someone please explain to me? Is my result correct?
Thanks in advance.


 
May be you have line-buffered stdout on AS400 system?
If so, try (ANSI/ISO Std lib func from <stdio.h>):
Code:
setvbuf(stdout,0,_IONBF,0); /* No buffering */
as the 1st executable statement in main().
May be it helps...
 
Hi,

I don't know if I have line-buffered stdout or not but I tried what you suggested anyway. It produced the same result. Here's where I inserted.

int main(void)
{
int month;
setvbuf(stdout,0,_IONBF,0);
printf("enter month: ");
...
...
 
Alas... I can't reproduce this strange behaviour on my Win station. Moreover, the prompt without '\n' is on my screen without setvbuf() too...
May be it's AS400 OS problem?
I hope you can realize less friendly seeing console dialog with \n at the end of a prompt message...
 
Code:
printf("enter month: ");
fflush( stdout );
Unless you print a \n at the end of some output, you need to call fflush() if you want to guarantee seeing it.


--
 
Try fflush(stdout); before the scanf() statement.
 
Thank you, it works now.
(I wonder why the tutorial books, some one gave to me and
another book from IBM, don't have fflush statement in
their samples.)

Thank you everyone for your help.
 
> (I wonder why the tutorial books, some one gave to me and
> another book from IBM, don't have fflush statement in
> their samples.)

Probably because it works fine without it for the person who wrote the tutorial. They assume that since it works most places, it works everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top