Thread: scanf can not get the values from keyboard.

  1. #1
    Registered User
    Join Date
    Feb 2011

    Unhappy scanf can not get the values from keyboard.



    I am new to C, I have coped the following code from a book.

    I used visaul C++ 2008 from Microsoft. the program is a console application.

    I entered a integer from keyboard, but I can see any output on the console.

    (the getchar () is just for the stop the screen, then I can see the results).

    where is wroung?

    Thanks for any helps!!


    #include <stdio.h>
    int main()
    int k=1;
     printf ("enter an integer  \n" );
     scanf("%d ", &k);
    printf (" the entered integer is  :  %d \n  ",  k);
    getchar ();
    return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You should remove the trailing spaces on your scanf (and printf) lines.
    "%d", not "%d "
    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. Scanf is changing my results!
    By rogster001 in forum C Programming
    Replies: 8
    Last Post: 08-18-2009, 12:26 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. keeping values of scanf
    By n00by in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 03:38 AM