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 198067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:54:24+00:00 2026-05-11T16:54:24+00:00

If I want to receive a one character input in C, how would I

  • 0

If I want to receive a one character input in C, how would I check to see if extra characters were sent, and if so, how would I clear that?

Is there a function which acts like getc(stdin), but which doesn’t prompt the user to enter a character, so I can just put while(getc(stdin)!=EOF);? Or a function to peek at the next character in the buffer, and if it doesn’t return NULL (or whatever would be there), I could call a(nother) function which flushes stdin?

Edit

So right now, scanf seems to be doing the trick but is there a way to get it to read the whole string, up until the newline? Rather than to the nearest whitespace? I know I can just put “%s %s %s” or whatever into the format string but can I handle an arbitrary number of spaces?

  • 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-05-11T16:54:24+00:00Added an answer on May 11, 2026 at 4:54 pm

    You cannot flush the input stream. You will be invoking undefined behavior if you do. Your best bet is to do:

    int main() {
      int c = getchar();
      while (getchar() != EOF);
      return 0;
    }
    

    To use scanf magic:

    #include <stdio.h>
    #include <stdlib.h> 
    
    #define str(s) #s
    #define xstr(s) str(s)
    #define BUFSZ 256
    
    int main() {
      char buf[ BUFSZ + 1 ];
      int rc = scanf("%" xstr(BUFSZ) "[^\n]%*[^\n]", buf);
      if (!feof(stdin)) {
        getchar();
      }
      while (rc == 1) {
        printf("Your string is: %s\n", array);
        fflush(stdout);
        rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
        if (!feof(stdin)) {
            getchar();
        }
       }
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to receive a function in parameter and call it with one parameter
One of my assignments in to write my own UNIX Shell. To receive input
I have this code, the point is that I want to receive no more
I want to receive data from an internal table which is a dynamic one.
Suppose I own say add-one.com and if I receive add-one.com/23 I want to capture
I want to create a WCF RESTful service method that can receive an arbitrary
In an applescript, I receive one filepath that I've to open. The filepath is
Given a really large, 3+ gig, binary file is there anyway that I could
I have been required to write a function that reads the BSDF data format
I have a series of data that I want to put inside an array

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.