LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-22-2004, 09:36 AM   #1
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Rep: Reputation: 30
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
 
Old 07-22-2004, 11:03 AM   #2
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
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 ... )
 
Old 07-22-2004, 12:10 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 07-22-2004, 02:09 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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!
 
Old 07-22-2004, 02:46 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 07-22-2004, 02:48 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 07-22-2004, 04:45 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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...
 
Old 07-22-2004, 08:18 PM   #8
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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
 
Old 07-22-2004, 09:10 PM   #9
Veteq
Member
 
Registered: Feb 2004
Posts: 155

Original Poster
Rep: Reputation: 30
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
 
Old 07-22-2004, 09:18 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 07-23-2004, 01:24 AM   #11
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
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.
 
Old 07-23-2004, 07:00 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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();
}
 
Old 07-23-2004, 08:22 AM   #13
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 07-23-2004, 03:49 PM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 07-23-2004, 03:59 PM   #15
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I was was referring to this loop that arvind suggested:
Code:
scanf ("%d", &num);
while (getchar () != '\n')
    ;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Using [code] inside of [color] results in very small text scuzzman LQ Suggestions & Feedback 1 03-28-2005 02:14 AM
Same code, two different results. (C++) Travis86 Programming 7 11-07-2004 04:36 PM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration