Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:25:21+00:00 2026-06-12T15:25:21+00:00

I have a difficulty understanding getchar() . In the following program getchar works as

  • 0

I have a difficulty understanding getchar(). In the following program getchar works as expected:

#include <stdio.h>


int main()
{
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

However, in the following program, getchar does not create a delay and the program ends:

#include <stdio.h>

int main()
{
    char command[100];
    scanf("%s", command );
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

I have the following weired workaround, which works, but I don’t understand why:

#include <stdio.h>

int main()
{
    char command[100];
    int i;
    scanf("%s", command );
    printf("Type Enter to continue...");
    while ( getchar() != '\n') {
      i=0; 
    }
    getchar();
    return 0;    
}

So my questions are:
1. What is scanf doing? Why does scanf do this ?
2. Why is my work around working?
3. What is a good way to emulate the following Python code:

raw_input("Type Enter to continue")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:25:22+00:00Added an answer on June 12, 2026 at 3:25 pm

    The input is only sent to the program after you typed a newline, but

    scanf("%s", command );
    

    leaves the newline in the input buffer, since the %s(1) format stops when the first whitespace character is encountered after some non-whitespace, getchar() then returns that newline immediately and doesn’t need to wait for further input.

    Your workaround works because it clears the newline from the input buffer before calling getchar() once more.

    To emulate the behaviour, clear the input buffer before printing the message,

    scanf("%s", command);
    int c;
    do {
        c = getchar();
    }while(c != '\n' && c != EOF);
    if (c == EOF) {
        // input stream ended, do something about it, exit perhaps
    } else {
        printf("Type Enter to continue\n");
        getchar();
    }
    

    (1) Note that using %s in scanf is very unsafe, you should restrict the input to what your buffer can hold with a field-width, scanf("%99s", command) will read at most 99 (sizeof(command) - 1)) characters into command, leaving space for the 0-terminator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little difficulty understanding alias_method / alias_method_chain . I have the following
I have encountered a difficulty in understanding the subroutine syntax. using the following code:
I'm a noob in objective-c (from C#/Java background), and have difficulty understanding following objective-c
I'm not a C++ programmer and have difficulty understanding the explanations given on websites.
i have a little difficulty in understanding how to do some INSERT SELECT. For
I have difficulty understanding SELECT * FROM myTable WHERE 0 = ... in the
I am new to Matlab and have difficulty understanding global variables. For example global
I'm kind of new in Haskell and I have difficulty understanding how inferred types
I have difficulty in understanding the use of union in C. I have read
I am having some difficulty understanding callbacks and program flow, synchronization issues. Lets say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.