ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# include <stdio.h>
int num;
main()
{
system("clear");
printf("enter any number: ");
fflush ( stdin);
scanf("%d", &num );
printf("the number entered was %d\n", num);
fflush(stdin);
printf("press any key\n");
getchar();
}
This is a very simple program to test a theory.
When I compile this code under AIX (CC compiler), the program waits at "Press any key" before exiting the program (getchar() command). The same code in linux (CC compiler) skips the pause and exits the program before any key is pressed.
what am I missing?
Also portability is not on of C's strong points. If portability is important to you use a language that ports well (shell script, Perl, Python, Java ... )
fflush() is not suppposed to be called on input streams, when u do call it u it may or may not do anything on the stream. im guessing that on linux it actually flushes stdin, and on aix it doesn't.
The problem is that the scanf() leaves a \n waiting on the input stream, and the getchar() grabs that immediately. I managed to make scanf take a single \n off the stream like this:
Code:
# include <stdio.h>
int num;
main()
{
system("clear");
printf("enter any number: ");
scanf("%d%*1[\n]", &num);
printf("the number entered was %d\n", num);
printf("press any key\n");
getchar();
}
clearly, still ugly as sin, but my man page seems to suggest that it is portable. I think AIX is simply doing the wrong thing.
When you actually want to make a screen-oriented app, this crap is probably the reason you're supposed to use ncurses.
Yes, a nice hack, but it fails for inputs like, "12 \n", ie, an extra space before the "\n". The actual portable way to do this (flush the input stream) is to use a while loop:
arvind_sw is probably right. the only question I have about it is that it seems from the OP that scanf was magically eating the \n on AIX and not linux, so if that's the case you could end up waiting in the wrong place. Then again if AIX is broken maybe mine would too. Mine definitely sucked about not eating random characters after the number, in any event.
Well.....I too tried many different things. But the best way I guess, is to use two getch() calls instead of one. The first one will collect the \n while the second will read the data for you.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
As said before, fflushing stdin is outside the standard C library specifications, so there is no point blaming O/S reliability or language portability there.
scanf is also unreliable when the input come from a user, as it may crash if, for example, non numeric values are entered.
Here is a version I guess is more portable
Code:
#include <stdio.h>
int num;
main()
{
char line[32];
system("clear");
printf("enter any number: ");
fgets(line, 31, stdin);
sscanf(line, "%d", &num);
printf("the number entered was %d\n", num);
printf("press any key\n");
getchar();
}
Instead of a loop you can also just use fgets() on stdin after the scanf(). Or just use fgets() in the first place and then use strtod() or atoi(). I really can't stand scanf() and I never use it on non-trusted input.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.