c beginners issue

This is a discussion on c beginners issue within the C Programming forums, part of the General Programming Boards category; I have a very small piece of code which will not run correctly. I need to input some data on ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    c beginners issue

    I have a very small piece of code which will not run correctly.
    I need to input some data on the key board before the printf statement will run.
    it appears that the scanf statement runs before the printf. why is this ?

    Code:
    #include <stdio.h>
    
    int main()
    {
      int number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &number );
    
    }
    thanks in advance
    trevfur

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    You don't flush the output stream.

    printf( "Please enter a number: " );
    fflush( stdout );
    scanf( "%d", &number );


    if you want consistent behaviour across all systems.

    Some systems will flush stdout at the start of accessing stdin, but I guess yours doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. for beginner's or anyone interested
    By Iconoklast in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2004, 02:45 PM
  4. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 05:02 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21