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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › convert-string-to-int-in-c
Convert String to int in C - GeeksforGeeks
July 23, 2025 - We can use sscanf() to easily convert a string to an integer. This function reads the formatted input from the string buffer, so we can use the format specifiers to read the numerical characters as integers.
Top answer
1 of 13
253

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 13
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

Discussions

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
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
How to convert integer to string in C? - Stack Overflow
@kasrak How to deal with that case. Is a larger type the only option? 2016-10-02T09:24:26.957Z+00:00 ... Save this answer. ... Show activity on this post. That's because itoa isn't a standard function. Try snprintf instead. ... At least enough to hold the maximum value allowed by the integer type, I guess. 2012-03-11T13:17:17.727Z+00:00 ... snprintf() is safer in that you specify how much input you're taking. Otherwise, If your string ... More on stackoverflow.com
🌐 stackoverflow.com
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
People also ask

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? 5 Ways With Code
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? 5 Ways With Code
Why do we need to convert string to int in C?
We need to convert string to int in C because user inputs or file data are usually strings, but numeric calculations require integers.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › string-to-int
How to Convert String to Int in C? 5 Ways With Code
🌐
Udemy
blog.udemy.com › home › it & development › software development › c string to int: simple ways to convert to numeric values
C String to Int: Simple Ways to Convert to Numeric Values - Udemy Blog
April 14, 2026 - 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? 5 Ways With Code
May 9, 2026 - Learn five easy ways to convert a string to an int in C using atoi(), strtol(), sscanf(), and manual conversion. Includes code examples!
🌐
Delft Stack
delftstack.com › home › howto › c string to int
How to Convert a String to Integer in C | Delft Stack
February 2, 2024 - We define a string str containing the numeric characters 123. We use atoi() to convert str to an integer and store the result in the value variable.
Find elsewhere
🌐
w3resource
w3resource.com › c-programming-exercises › variable-type › c-variable-type-exercises-13.php
C Program: Convert a string to an integer - w3resource
July 29, 2025 - Write a C program to convert a string to an integer. ... #include<stdio.h> // Include the standard input/output header file. #include<stdlib.h> // Include the standard library header file. int main () // Start of the main function. { int num1; // Declare an integer variable 'num1'. char my_array[256]; // Declare a character array 'my_array' with a maximum size of 256.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-a-string-to-a-integer-in-c
How to convert a string to a integer in C
March 15, 2026 - #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *name = "The Matrix(1999)"; char *ps; char year_str[5] = ""; int year, i; /* Find the opening bracket */ ps = strchr(name, '('); if (ps != NULL) { /* Extract characters between brackets */ for (i = 1; i < strlen(ps) - 1 && i < 5; i++) { year_str[i-1] = ps[i]; } year_str[4] = '\0'; /* Convert to integer */ year = atoi(year_str); printf("Movie: %s<br>", name); printf("Year: %d<br>", year); } return 0; }
🌐
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 ...
🌐
TutorialKart
tutorialkart.com › c-programming › how-to-convert-a-string-to-an-integer-in-c
How to Convert a String to an Integer in C
February 20, 2025 - The atoi() function (ASCII to integer) is a simple method to convert a string to an integer. It is part of the stdlib.h library. However, it does not provide error handling for invalid inputs.
🌐
Blogger
blockofcodes.blogspot.com › 2013 › 07 › how-to-convert-string-to-integer-in-c.html
Block Of Codes: How to convert string to integer in C
We have a built in function in C header file stdlib.h to convert string to integer. And the function is int atoi(const char *str) .
🌐
Blogger
blockofcodes.blogspot.com › 2013 › 07 › how-to-convert-string-to-integer-in-c.html
How to convert string to integer in C - Block Of Codes
July 31, 2013 - We have a built in function in C header file stdlib.h to convert string to integer. And the function is int atoi(const char *str) .
🌐
GitHub
gist.github.com › shamiul94 › 02d4957f410b138ef8aca6b7602d12b6
String to Integer Conversion.c · GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/shamiul94/02d4957f410b138ef8aca6b7602d12b6.js&quot;&gt;&lt;/script&gt; Save shamiul94/02d4957f410b138ef8aca6b7602d12b6 to your computer and use it in GitHub Desktop. Download ZIP · Raw · String to Integer Conversion.c ·
🌐
Sololearn
sololearn.com › en › Discuss › 1972062 › how-to-convert-any-string-to-integer
How to convert any string to integer | Sololearn: Learn to code for FREE!
using atoi function is just a bad way to change string to integers. as Ace had mentioned earlier, i'll show you my personal way of doing this properly and safely using standard functions. and without remembering all the functions mentioned by Martin Taylor String to Integer char string[] = "123"; int num; sscanf(string, "%d", &num); printf("num = %d", num); >>> num = 123 Integer to String char string[4]; int num = 456; sprintf(string, "%d", num); printf("string = %s", string); >>> string = 456 as u can see sscanf and sprintf replicates the behaviour of scanf and printf only that the input and output goes to a variable instead of the user input and console output. ... Type in a code to convert the string into an integer: char str_num[] = "123"; int num = _____ (str_num); printf("%d", num); Answer atoi
🌐
Sanfoundry
sanfoundry.com › c-program-integer-to-string-vice-versa
C Program to Convert Integer to String and Vice-versa - Sanfoundry
May 13, 2022 - The toint() function is used to convert an integer to string & vice-versa. Assign the length of the string to ‘len’ variable. For loop is used to convert the string to an integer.
🌐
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
Answer (1 of 5): As another answer has it, [code ]stoi()[/code] (or [code ]atoi()[/code], if you have a C-style char pointer instead of a string) from the standard library is the way to go. For those of you wondering how these functions work, here’s a quick explanation.