There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it's not portable):

long long
     strtonum(const char *nptr, long long minval, long long maxval,
     const char **errstr);

You might also be interested in strtoumax and strtoimax which are standard functions in C99. For example you could say:

uintmax_t num = strtoumax(s, NULL, 10);
if (num == UINTMAX_MAX && errno == ERANGE)
    /* Could not convert. */

Anyway, stay away from atoi:

The call atoi(str) shall be equivalent to:

(int) strtol(str, (char **)NULL, 10)

except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.

Answer from cnicutar on Stack Overflow
Top answer
1 of 15
252

There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it's not portable):

long long
     strtonum(const char *nptr, long long minval, long long maxval,
     const char **errstr);

You might also be interested in strtoumax and strtoimax which are standard functions in C99. For example you could say:

uintmax_t num = strtoumax(s, NULL, 10);
if (num == UINTMAX_MAX && errno == ERANGE)
    /* Could not convert. */

Anyway, stay away from atoi:

The call atoi(str) shall be equivalent to:

(int) strtol(str, (char **)NULL, 10)

except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.

2 of 15
46

Robust C89 strtol-based solution

With:

  • no undefined behavior (as could be had with the atoi family)
  • a stricter definition of integer than strtol (e.g. no leading whitespace nor trailing trash chars)
  • classification of the error case (e.g. to give useful error messages to users)
  • a "testsuite"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

typedef enum {
    STR2INT_SUCCESS,
    STR2INT_OVERFLOW,
    STR2INT_UNDERFLOW,
    STR2INT_INCONVERTIBLE
} str2int_errno;

/* Convert string s to int out.
 *
 * @param[out] out The converted int. Cannot be NULL.
 *
 * @param[in] s Input string to be converted.
 *
 *     The format is the same as strtol,
 *     except that the following are inconvertible:
 *
 *     - empty string
 *     - leading whitespace
 *     - any trailing characters that are not part of the number
 *
 *     Cannot be NULL.
 *
 * @param[in] base Base to interpret string in. Same range as strtol (2 to 36).
 *
 * @return Indicates if the operation succeeded, or why it failed.
 */
str2int_errno str2int(int *out, char *s, int base) {
    char *end;
    if (s[0] == '\0' || isspace((unsigned char) s[0]))
        return STR2INT_INCONVERTIBLE;
    errno = 0;
    long l = strtol(s, &end, base);
    /* Both checks are needed because INT_MAX == LONG_MAX is possible. */
    if (l > INT_MAX || (errno == ERANGE && l == LONG_MAX))
        return STR2INT_OVERFLOW;
    if (l < INT_MIN || (errno == ERANGE && l == LONG_MIN))
        return STR2INT_UNDERFLOW;
    if (*end != '\0')
        return STR2INT_INCONVERTIBLE;
    *out = l;
    return STR2INT_SUCCESS;
}

int main(void) {
    int i;
    /* Lazy to calculate this size properly. */
    char s[256];

    /* Simple case. */
    assert(str2int(&i, "11", 10) == STR2INT_SUCCESS);
    assert(i == 11);

    /* Negative number . */
    assert(str2int(&i, "-11", 10) == STR2INT_SUCCESS);
    assert(i == -11);

    /* Different base. */
    assert(str2int(&i, "11", 16) == STR2INT_SUCCESS);
    assert(i == 17);

    /* 0 */
    assert(str2int(&i, "0", 10) == STR2INT_SUCCESS);
    assert(i == 0);

    /* INT_MAX. */
    sprintf(s, "%d", INT_MAX);
    assert(str2int(&i, s, 10) == STR2INT_SUCCESS);
    assert(i == INT_MAX);

    /* INT_MIN. */
    sprintf(s, "%d", INT_MIN);
    assert(str2int(&i, s, 10) == STR2INT_SUCCESS);
    assert(i == INT_MIN);

    /* Leading and trailing space. */
    assert(str2int(&i, " 1", 10) == STR2INT_INCONVERTIBLE);
    assert(str2int(&i, "1 ", 10) == STR2INT_INCONVERTIBLE);

    /* Trash characters. */
    assert(str2int(&i, "a10", 10) == STR2INT_INCONVERTIBLE);
    assert(str2int(&i, "10a", 10) == STR2INT_INCONVERTIBLE);

    /* int overflow.
     *
     * `if` needed to avoid undefined behaviour
     * on `INT_MAX + 1` if INT_MAX == LONG_MAX.
     */
    if (INT_MAX < LONG_MAX) {
        sprintf(s, "%ld", (long int)INT_MAX + 1L);
        assert(str2int(&i, s, 10) == STR2INT_OVERFLOW);
    }

    /* int underflow */
    if (LONG_MIN < INT_MIN) {
        sprintf(s, "%ld", (long int)INT_MIN - 1L);
        assert(str2int(&i, s, 10) == STR2INT_UNDERFLOW);
    }

    /* long overflow */
    sprintf(s, "%ld0", LONG_MAX);
    assert(str2int(&i, s, 10) == STR2INT_OVERFLOW);

    /* long underflow */
    sprintf(s, "%ld0", LONG_MIN);
    assert(str2int(&i, s, 10) == STR2INT_UNDERFLOW);

    return EXIT_SUCCESS;
}

GitHub upstream.

Based on: https://stackoverflow.com/a/6154614/895245

🌐
GeeksforGeeks
geeksforgeeks.org › c language › convert-string-to-int-in-c
Convert String to int in C - GeeksforGeeks
July 23, 2025 - There are 4 methods to convert a string to int which are as follows: ... The atoi() function in C takes a character array or string literal as an argument and returns corresponding value as integer. It is defined in the <stdlib.h> header file.
Discussions

Beginner needing help, converting string to integer.
What you want is stoi but since you can't use it, you will have to manually parse the string. The way it goes is, we inspect each individual character in string and if it is between '0' (0x30) and '9' (0x39), we subtract '0' from this value. Say the character is '4' (0x34) so the arithmatic will be 0x34 - 0x30 = 4 We do this for every letter and we have integer value. So something like define variable num start for loop from 0 till str.size() if character is between '0' and '9', subtract '0' and save in val num = num*10 + val end for loop More on reddit.com
🌐 r/Cplusplus
17
4
November 9, 2022
is there any function in c to convert an integer to a string?
atoi() converts string to int, itoa() converts int to string. Still have no clue what 'a' is More on reddit.com
🌐 r/learnprogramming
38
19
December 30, 2022
Converting char/string to int?
Do you notice any warnings about your code when you compile it? Specifically these lines: int toInt1 = string1; int toInt2 = string2; For a quick solution, look up the function, atoi. You can use it to convert your strings to ints. In general, the function, strtol, is probably what you want to learn about for converting strings to integers. You might also find sscanf useful. Also, read up on printf. The %s is only for strings. Use %d for ints. More on reddit.com
🌐 r/C_Programming
7
0
June 19, 2018
string to int
You can treat a single character as a number without doing any special conversions: #include int main(void) { int x = 100 - 'a'; printf("100 - 'a' = %d\n",x); char c2[6] = {104, 101, 108, 108, 111, 0}; printf("%s\n", c2); } You can't directly convert a string to an ASCII value, because... Well that just doesn't really make sense. A string doesn't have an ASCII value. Each character in the string does. If you just want to do math on the characters of a string, you can go right ahead. It's perfectly valid to do arithmetic on a char. You can also freely assign the value of a char to an integer type: int x = 'a' + 'c'; More on reddit.com
🌐 r/cs50
8
1
October 11, 2023
People also ask

What is the easiest way to convert string to int in C?
The easiest way to convert string to int in C is by using the built-in atoi() function from the library.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › string-to-int
How to Convert String to Int in C? 4 Ways With Code
Can I convert string to integer in C without using atoi function?
Yes, you can manually convert string to integer in C without using atoi function by implementing loops and ASCII character manipulation.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › string-to-int
How to Convert String to Int in C? 4 Ways With Code
Can we convert floating-point strings to integers directly in C?
No, directly converting floating-point strings like "12.34" to integer is not possible. You must first convert them to float and then cast to integer.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › string-to-int
How to Convert String to Int in C? 4 Ways With Code
🌐
Udemy
blog.udemy.com › home › c string to int: simple ways to convert to numeric values
C String to Int: Simple Ways to Convert to Numeric Values - Udemy Blog
October 25, 2025 - In this case, the string is an array of characters pointed to by num. Then, we calculate the length of the string using the strlen() function. Next, we loop through the string and convert the string into decimal values.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › string-to-int
How to Convert String to Int in C? 4 Ways With Code
October 23, 2025 - Learn four easy ways to convert a string to an int in C using atoi(), strtol(), sscanf(), and manual conversion. Includes code examples!
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › converting char/string to int?
r/C_Programming on Reddit: Converting char/string to int?
June 19, 2018 -

Hey all. I am trying to convert strings/chars to ints for a number guessing game. I have created this simple program that is supposed to start out with two character arrays and then convert them into an int, then add the two. It doesn't seem to be working. I get no output. Now I am pretty new to C. I am used to JavaScript where you can just sling data types anywhere you want, so this concept in C is giving me some trouble. Any help would be great! Thanks!

Code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main(){

char string1[5] = "1234";

char string2[5] = "1000";

printf("String one says: %s \n", string1);

printf("String two says: %s \n \n", string2);

int toInt1 = string1;

int toInt2 = string2;

int add_ints = toInt1 + toInt2;

printf("String one should now be an int: %s \n", toInt1);

printf("String two should now be an int: %s \n", toInt2);

printf("So adding them together should produce: %s", add_ints);

}

🌐
Quora
quora.com › How-do-I-convert-a-part-of-a-string-into-int-in-C
How to convert a part of a string into int in C - Quora
Answer (1 of 3): I’ll take this in the reverse order. Once you have the ‘part’ of the string that you want, you pass it into [code ]atoi[/code] to convert it to an int. Getting the part you want is a bit more challenging, but there are a few general tips that can help you: 1. Mention the desir...
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › stdlib_h › strtol.php
C Language: strtol function (Convert String to Long Integer)
March 3, 2016 - In the C Programming Language, the strtol function converts a string to a long integer. The strtol function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number.
🌐
Codecademy
codecademy.com › article › how-to-convert-a-string-to-an-integer-in-c
How to Convert a String to an Integer in C++ | Codecademy
If you’re looking for a more straightforward method for converting strings to integers without parsing complex content, let’s explore atoi(). atoi() (ASCII to integer) is a legacy function inherited from the C programming language.
🌐
Sentry
sentry.io › sentry answers › c++ › convert char to int in c and c++
Convert char to int in C and C++ | Sentry
June 2, 2017 - The easiest way to convert a single character to the integer it represents is to subtract the value of '0'. If we take '3' (ASCII code 51) and subtract '0' (ASCII code 48), we’ll be left with the integer 3, which is what we want.
🌐
Reddit
reddit.com › r/cs50 › string to int
r/cs50 on Reddit: string to int
October 11, 2023 -

I want to convert string to ASCII. I thought something like this: string letter; letter = GetString(); int ascii = name; should work, but it doesn't a.c:10:9: error: incompatible pointer to integer conversion initializing 'int' with an expression of type 'string' (aka 'char *') [-Werror,-Wint-conversion]

Meaby someone can give me a hint, I want to use it in pset2 if this matter.

Top answer
1 of 4
2
You can treat a single character as a number without doing any special conversions: #include int main(void) { int x = 100 - 'a'; printf("100 - 'a' = %d\n",x); char c2[6] = {104, 101, 108, 108, 111, 0}; printf("%s\n", c2); } You can't directly convert a string to an ASCII value, because... Well that just doesn't really make sense. A string doesn't have an ASCII value. Each character in the string does. If you just want to do math on the characters of a string, you can go right ahead. It's perfectly valid to do arithmetic on a char. You can also freely assign the value of a char to an integer type: int x = 'a' + 'c';
2 of 4
2
Okay so I'm not really sure what you want. Converting strings to integers doesn't really make sense. Did you mean either of the following? Converting a string to an array of corresponding ASCII values stored as integers Strings are just arrays of characters so we can just iterate through them with a for loop. string str = "Hello"; int array_of_ascii[5]; for (int i = 0; i < 5; i++) { array_of_ascii[i] = (int) str[i]; } // array_of_ascii looks like this in memory: ------------------------------ | 72 | 101 | 108 | 108 | 111 | ------------------------------ Turning a string like "1234" into the integer 1234 There are several functions in the standard library which can do this. While just starting out, you can use the atoi() function (though this should almost never be used in "real" programs), like so. #include ... string str = "1234"; int i = atoi(str); printf("%d", i + 1); // prints 1235
🌐
Quora
quora.com › How-do-I-convert-a-string-to-an-int-in-C
How to convert a string to an int in C++ - Quora
You want to convert string which holds a non numeric value to it's ASCII equivalent, a No but you can convert individual characters to their ASCII values. You can do type casting, though it may have some unexpected results but will work fine for your example ... If you want to do it for the whole string, employ a loop but do it with style (modern style) Requires a C++11/14 compatible compiler ... You can use it to convert numeric strings to Integer and double as well and vice versa, using C++11's to_string and stoull,stoi and stod.
🌐
Quora
quora.com › What-are-the-best-ways-to-convert-a-string-into-a-number-with-C
What are the best ways to convert a string into a number with C? - Quora
Answer (1 of 4): Using the built-in function atoi which means ASCII to integer: [code]#include int main() { char input[40]; scanf("%s", input); int number = atoi(input); printf("%d\n", number); return 0; } [/code]Creating the atoi function is ...
🌐
Cplusplus
cplusplus.com › forum › general › 208135
string to int - C++ Forum
February 10, 2017 - Since you seem to be using C++ prefer c++ strings and getline() or the extraction operator. Then once you have your "number" in the string you can use stoi() to convert the string to an actual number. Or just get the numeric value directly into the correct type of variable instead of the string.
🌐
Quora
quora.com › How-do-I-convert-int-to-string-C
How to convert int to string C - Quora
Writes formatted data without checking bounds. ... There are several standard, portable ways to convert an int to a string in C.