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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:16:43+00:00 2026-05-13T13:16:43+00:00

gcc 4.4.2 I was reading an article about scanf. I personally have never checked

  • 0

gcc 4.4.2

I was reading an article about scanf. I personally have never checked the return code of a scanf.

#include <stdio.h>

int main(void)
{
  char buf[64];
  if(1 == scanf("%63s", buf))
  {
    printf("Hello %s\n", buf);
  }
  else
  {
    fprintf(stderr, "Input error.\n");
  }
  return 0;
}

I am just wondering what other techniques experienced programmers do when they use scanf when they want to get user input? Or do they use another function or write their own?

Thanks for any suggestions,

EDIT =========

#include <stdio.h>

int main(void)
{
    char input_buf[64] = {0};
    char data[64] = {0};

    printf("Enter something: ");
    while( fgets(input_buf, sizeof(input_buf), stdin) == NULL )
    {
    /* parse the input entered */
    sscanf(input_buf, "%s", data);
    }

    printf("Input [ %s ]\n", data);

    return 0;
}

I think most programmers agree that scanf is bad, and most agree to use fgets and sscanf. However, I can use fgets to readin the input. However, if I don’t know what the user will enter how do I know what to parse. For example, like if the user was to enter their address which would contain numbers and characters and in any order?

  • 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-13T13:16:43+00:00Added an answer on May 13, 2026 at 1:16 pm

    Don’t use scanf directly. It’s surprisingly hard to use. It’s better to read an entire line of input and to then parse it (possibly with sscanf).

    Read this entry (and the entries it references) from the comp.lang.c FAQ:
    http://c-faq.com/stdio/scanfprobs.html

    Edit:
    Okay, to address your additional question from your own edit: If you allow unstructured input, then you’re going to have to attempt to parse the string in multiple ways until you find one that works. If you can’t find a valid match, then you should reject the input and prompt the user again, probably explaining what format you want the input to be in.

    For anything more complicated, you’d probably be better off using a regular expression library or even using dedicated lexer/parser toolkits (e.g. flex and bison).

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

Sidebar

Related Questions

I've been doing some reading into designing template code have a question about it.
I was reading about the flags used in gcc, and read a reccommendation to
gcc 4.4.2 I have the following code: char channels[] = NumberOfChannel = [2]; sscanf(channels,
I'm reading the gcc manual at the moment, especially the part about warning/error flags.
I am reading an old book about code obfuscation in C (the book was
I'm reading over this awesome article on friday.com. bbum shows some objective-c code and
I have been reading about streams, but when I try and deviate from the
I was reading some samples of code, and they returned a const int. When
So I was reading http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html , and came across this: type __sync_and_and_fetch (type *ptr,
I am trying to unpackage gcc-4.4_4.4.3.orig.tar.gz. After reading 5 sites from Google to do

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.