Thread: c beginners issue

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

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, 04: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, 04:02 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM