Thread: new to C, need some easy help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Illinois
    Posts
    1

    new to C, need some easy help

    Hi guys, new to the forums here and liking what I see. I've been going through the tutorial section and ran into a problem with one of the programs. I'm confused as to why I need to use 2 getchar() funtions to get my program to pause correctly. If I only use one, as the tutorial suggests, it just opens and closes the command window super fast. Thx in advance! Here's the code:

    Code:
    #include <stdio.h>
    int main()
    {
        int myNum;
        
        printf("Enter a number: ");
        scanf("&#37;d", &myNum);
        getchar();
        printf("You entered %d", myNum);
        getchar();
    }
    EDIT: If it helps, I'm using the Bloodshed Dev-C++ IDE
    Last edited by amd84; 04-13-2008 at 12:15 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you type the number, and press ENTER, and use scanf to remove the integer, the newline character (from pressing ENTER), is still on the input stack. The first getchar remove the newline character. The second one causes the pause.

    Step through the code in your debugger and you will see it.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM