Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
07-22-2004, 09:36 AM
|
#1
|
Member
Registered: Feb 2004
Posts: 155
Rep:
|
Same C code different results
Code:
# 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?
please help
Veteq
|
|
|
07-22-2004, 11:03 AM
|
#2
|
Member
Registered: Jun 2004
Posts: 129
Rep:
|
what are the terminal settings?
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 ... )
|
|
|
07-22-2004, 12:10 PM
|
#3
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
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.
|
|
|
07-22-2004, 02:09 PM
|
#4
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
I've found that when using getchar, it will often pass imidiadtly from the keypress done of scanf.
Try using two getchars.
That is pretty cheesy though.  Oh well! I think that is the problem, its up to you to figure out how to fix it.
Sorry!
|
|
|
07-22-2004, 02:46 PM
|
#5
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
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.
|
|
|
07-22-2004, 02:48 PM
|
#6
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
oh, and the "any key" needs to be enter because your terminal is going to be line buffered. again there's some way to fix this with curses.
|
|
|
07-22-2004, 04:45 PM
|
#7
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
Originally posted by aluser
I managed to make scanf take a single \n off the stream like this:
Code:
scanf("%d%*1[\n]", &num);
clearly, still ugly as sin, but my man page seems to suggest that it is portable.
|
Nice hack. Who would think of that...
|
|
|
07-22-2004, 08:18 PM
|
#8
|
Member
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96
Rep:
|
Hi,
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:
Code:
scanf ("%d", &num);
while (getchar () != '\n')
;
Arvind
|
|
|
07-22-2004, 09:10 PM
|
#9
|
Member
Registered: Feb 2004
Posts: 155
Original Poster
Rep:
|
Thank you for all the help,
I am going to try a few of the sugestions.....
Once again thank you, I always find the answer here ....in this forum
|
|
|
07-22-2004, 09:18 PM
|
#10
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
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.
|
|
|
07-23-2004, 01:24 AM
|
#11
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
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.
|
|
|
07-23-2004, 07:00 AM
|
#12
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
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();
}
|
|
|
07-23-2004, 08:22 AM
|
#13
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
07-23-2004, 03:49 PM
|
#14
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Quote:
Instead of a loop you can also just use fgets() on stdin after the scanf()
|
There is no loop in what I suggest ??
Quote:
I really can't stand scanf() and I never use it on non-trusted input.
|
sscanf hasn't the issues scanf has, and can reliably be used, I agree atoi or strtod are enough for this simple case though.
Last edited by jlliagre; 07-23-2004 at 03:50 PM.
|
|
|
07-23-2004, 03:59 PM
|
#15
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
I was was referring to this loop that arvind suggested:
Code:
scanf ("%d", &num);
while (getchar () != '\n')
;
|
|
|
All times are GMT -5. The time now is 08:54 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|