You can use sprintf to do it, or maybe snprintf if you have it:

char str[ENOUGH];
sprintf(str, "%d", 42);

Where the number of characters (plus terminating char) in the str can be calculated using:

(int)((ceil(log10(num))+1)*sizeof(char))
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

People also ask

How to convert int to string in C easily?
You can easily convert int to string in C using the built-in sprintf() function from or the itoa() function (if supported).
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-string
How to Convert Int to String in C? 3 Ways With Code
Is itoa() function standard for integer to string conversion in C?
No, the itoa() function is non-standard and not available on all compilers, so use sprintf() for better portability.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-string
How to Convert Int to String in C? 3 Ways With Code
What’s the use case of integer to string conversion in C programming?
Typical use cases include displaying numeric data, formatting output, storing integers as text in files, and user interfaces.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-string
How to Convert Int to String in C? 3 Ways With Code
🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › int-to-string
How to Convert Int to String in C? 3 Ways With Code
October 23, 2025 - Learn three simple ways to convert an int to a string in C using sprintf(), itoa(), and other methods. Includes code examples!
🌐
GeeksforGeeks
geeksforgeeks.org › c language › convert-string-to-int-in-c
Convert String to int in C - GeeksforGeeks
July 23, 2025 - Start traversing the string str from the front. For each character ch, check if they are numeric or non-numeric ASCII value using the range of numbers i.e. [48, 57]. ... Convert it to corresponding integer value using offset (-48) or simply '0'.
Find elsewhere
🌐
Quora
quora.com › How-do-I-convert-int-to-string-C
How to convert int to string C - Quora
Answer (1 of 8): There are two ... in c++ 1 Using built in function available in string header file int num = 100; // a variable of int data type string str; // a variable of str data type // using to_string to convert an int into a string str = to_string(num); ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
What’s the best way to convert int to string C when writing struct data to a file? - TestMu AI Community
June 2, 2025 - I’m working with structs in C and want to save their data as strings in a file. For that, I need to convert some integer values into strings. What’s the standard or safest way to perform int to string C conversion? Shou…
🌐
Delft Stack
delftstack.com › home › howto › how to convert an integer to a string in c
How to Convert an Integer to a String in C | Delft Stack
February 12, 2024 - This can be useful when you need to display numbers, log information, or construct strings that include numeric values. Here, we will look at three common methods for this conversion: sprintf(), snprintf(), and itoa(). As its name suggests, this function prints any value into a string.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Help with int to string - Programming - Arduino Forum
November 24, 2021 - HI, I'm a complete putz with C programming but can someone show me how to get this to work. I'm simply trying to get three integers into a string called reading. int a0 = 124; int a1 = 2315; int a2 = 567; String…
🌐
Internshala
trainings.internshala.com › home › programming › convert int to string in c++
How to Convert Int to String in C++: A Comprehensive Guide
January 16, 2024 - Learn how to convert int to string in C++. Know different methods like stringstream, to_string(), and sprintf() and how to initialize and declare strings.
🌐
Quora
quora.com › What-is-the-process-for-converting-an-int-to-a-string-in-the-C-programming-language-Is-there-a-built-in-function-available-for-this-purpose
What is the process for converting an int to a string in the C programming language? Is there a built-in function available for this purpose? - Quora
Answer (1 of 6): Indeed, as other answers have shown, there are built-in functions for this (such as sprintf). However, writing your own function to do this is quite easy. I wrote the below function a while back: [code]template void to_string(T val, char *buffer) { if (!va...
🌐
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 - C String to Int: Learn some of the ways you can convert a string to an integer in the C programming language.
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-an-integer-in-c
How to convert a string to an integer in C
If successfully executed, the function returns the integer value. If the string starts with an alphanumeric character or only contains alphanumeric characters, 0 is returned. In case the string starts with a numeric character but is followed by an alpha-numeric character, the string is converted to an integer until the occurrence of the first alphanumeric character
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Convert int to string and print in serial monitor - Programming - Arduino Forum
April 11, 2024 - So I am receiving data over bluetooth which is in the form of ascii characters. So 255 comes in as 50,53,53 So what I need to do is convert the intergers into strings then concatenate the strings together then convert the new string back into a int. So myRecievednumbers is an array and in the ...