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

  • Home
  • SEARCH
  • 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 7774843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:28:51+00:00 2026-06-01T17:28:51+00:00

I want to confirm if a value returned from the scanf() function is a

  • 0

I want to confirm if a value returned from the scanf() function is a floating number or not. How can I do that? My code is not running as it should if the wrong data types are supplied to the scanf() function. Similarly, how would I confirm if a value returned is a character string or not?

  • 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-01T17:28:52+00:00Added an answer on June 1, 2026 at 5:28 pm

    scanf() et al return the number of successful conversions.

    If you have:

    scanf("%f", &f);
    

    you should test:

    if (scanf("%f", &f) == 1)
        ...all OK...
    else
        ...EOF or conversion failure...
    

    If you have several conversions, check that they all completed. If you’re using %n ‘conversions’, they aren’t counted.

    Although scanf() does return EOF on EOF, you should not test for that — you should always check primarily that you got the number of conversions you expected. For example, consider the buggy code:

    while (scanf("%f %d %s", &f, &i, s) != EOF)  // Here be BUGS!
        ...loop body...
    

    If you type 3.14 x23 yes, then you will have an infinite loop because scanf() will return 1 on the first iteration (it successfully converted 3.14), and 0 thereafter (not EOF).

    You might be OK with:

    while ((rc = scanf("%f %d %s", &f, &i, s)) != EOF)
    {
        if (rc != 3)
            ...oops data problems...
        else
            ...all OK...
    }
    

    Judging from previous questions, you should be looking at using fgets() (or possibly POSIX getline()) to read lines of data, and then using sscanf() or even functions like strtol() and strtod() to read particular values from the line. If you use sscanf(), the comments made above about checking the number of successful conversions still apply.

    I don’t use scanf() in production code; it is just too damn hard to control properly. I regard it as almost suitable for beginning programs — except that it causes lots of confusion. On the whole, the best advice is ‘stay clear of scanf() and fscanf()‘. Note that that does not mean you have to stay clear of sscanf(), though some caution is needed even with sscanf().

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

Sidebar

Related Questions

For the following code <script type=text/javascript> $(document).ready(function(){ $(#erase).click(function(){ confirm('Are you sure you want to
How can i get the value that was pressed in the confirm box? <script
I want to set a variable from a flash file so that I can
I want to write something that acts just like confirm() in javascript, but I
I want to change the value of data-confirm attribute on a button (submit) based
I just wrote a confirm delete function that requires a class name: jQuery(document).ready(function($) {
Just a quick question to confirm the documentation: The getY(int) function from MotionEvent in
In android how to search that sharedpreference contains some value or not? Actually I
I can create a confirm box in Ext JS like this: with this code:
The following code is just a demonstration for the theory that I want to

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.