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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:38:00+00:00 2026-05-16T02:38:00+00:00

gcc 4.4.4 c89 What is better to convert a string to an integer value.

  • 0

gcc 4.4.4 c89

What is better to convert a string to an integer value.

I have tried 2 different methods atoi and sscanf. Both work as expected.

char digits[3] = "34";
int device_num = 0;

if(sscanf(digits, "%d", &device_num) == EOF) {
    fprintf(stderr, "WARNING: Incorrect value for device\n");
    return FALSE;
}

or using atoi

device_num = atoi(digits);

I was thinking that the sscanf would be better as you can check for errors. However, atoi doesn’t doing any checking.

  • 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-16T02:38:01+00:00Added an answer on May 16, 2026 at 2:38 am

    You have 3 choices:

    1. atoi

    This is probably the fastest if you’re using it in performance-critical code, but it does no error reporting. If the string does not begin with an integer, it will return 0. If the string contains junk after the integer, it will convert the initial part and ignore the rest. If the number is too big to fit in int, the behaviour is unspecified.

    1. sscanf

    Some error reporting, and you have a lot of flexibility for what type to store (signed/unsigned versions of char/short/int/long/long long/size_t/ptrdiff_t/intmax_t).

    The return value is the number of conversions that succeed, so scanning for "%d" will return 0 if the string does not begin with an integer. You can use "%d%n" to store the index of the first character after the integer that’s read in another variable, and thereby check to see if the entire string was converted or if there’s junk afterwards. However, like atoi, behaviour on integer overflow is unspecified.

    1. strtol and family

    Robust error reporting, provided you set errno to 0 before making the call. Return values are specified on overflow and errno will be set. You can choose any number base from 2 to 36, or specify 0 as the base to auto-interpret leading 0x and 0 as hex and octal, respectively. Choices of type to convert to are signed/unsigned versions of long/long long/intmax_t.

    If you need a smaller type you can always store the result in a temporary long or unsigned long variable and check for overflow yourself.

    Since these functions take a pointer to pointer argument, you also get a pointer to the first character following the converted integer, for free, so you can tell if the entire string was an integer or parse subsequent data in the string if needed.


    Personally, I would recommend the strtol family for most purposes. If you’re doing something quick-and-dirty, atoi might meet your needs.

    As an aside, sometimes I find I need to parse numbers where leading whitespace, sign, etc. are not supposed to be accepted. In this case it’s pretty damn easy to roll your own for loop, eg.,

    for (x=0; (unsigned)*s-'0'<10; s++) 
        x=10*x+(*s-'0');
    

    Or you can use (for robustness):

    if (isdigit(*s))
        x=strtol(s, &s, 10);
    else /* error */ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.1 c89 I have 2 different structures called origin_t and session_t. I would
gcc 4.4.4 c89 Is there a better way to do this? I have the
gcc 4.4.2 c89 I have a wave file: 8000 Hz 16 bit I am
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
gcc 4.4.2 c89 I have the following enum: enum drop_options_e { drop_ssm, drop_snm, drop_ssb
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
gcc 4.4.1 c89 I have the following code: static enum states { ACTIVE, RUNNING,
gcc 4.4.3 c89 I have the following code as a sample of what I
gcc 4.4.3 c89 I have the following source code. And getting a stack dump
gcc 4.4.4 c89 What is the standard way to null terminate a string? When

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.