How to convert integer to string in C?

I tried this example:

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,10);
    printf ("decimal: %s\n",buffer);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);
    itoa (i,buffer,2);
    printf ("binary: %s\n",buffer);
    return 0;

but the example there doesn't work (it says the function itoa doesn't exist).


Use sprintf():

int someInt = 368;
char str[12];
sprintf(str, "%d", someInt);

All numbers that are representable by int will fit in a 12-char-array without overflow, unless your compiler is somehow using more than 32-bits for int. When using numbers with greater bitsize, e.g. long with most 64-bit compilers, you need to increase the array size—at least 21 characters for 64-bit types.

Making your own itoa is also easy, try this :

char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
        *p++ = '-';
        i *= -1;
    int shifter = i;
    do{ //Move to where representation ends
        shifter = shifter/10;
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    return b;

or use the standard sprintf() function.

That's because itoa isn't a standard function. Try snprintf instead.

char str[LEN];
snprintf(str, LEN, "%d", 42);
Tags: C / String / Integer

Similar questions

How to convert integer to string to print integer on UART
I want to print out an integer value which is computed in the program on a uart screen. I am basically creating a data logging system. The user must be able to view the times when a character arrives. I am not able to use printf or snprintf or sprint as i am creating a time triggered system and printf takes some time to execute. I have searched sta...
Convert char String to an int but not possible to convert a char from an char string to an int?
Is there a solution to convert an char "slice" of an char string into an int? Short description: User inputs a string with values for example: 1, 2 3 4 ,5 Iam formating that string to 12345 With each number i want to continue to work with the index of an array.
How to convert binary string to integer string
Possible Duplicate: Converting string of 1s and 0s into binary value Lets say that I have string which contains 1024 characters (represents 0 and 1). I want to present it as an number in decimal base (also as a string). The tricky part is that i have to do it in C/C++ without 3rd part libraries. Any clues?
C++ C
How to convert a signed integer to a corresponding unsigned integer in C?
I'd like to define a C macro , which takes a signed integer x (can be: signed char, short, int, long, long long, or anything else, even something longer than a long long), and it converts x to the corresponding unsigned integer type of the same size. It's OK to assume that signed integers use the two's complement representation. So to convert any v...
Convert character to integer, and integer back to same character
I want two create two functions that can do this. So one function takes a character, for example the character a and returns the integer 97. The other function takes this integer 97 and returns the character a. I know this can be done by using the ASCII codes of these characters, but then it wouldn't work for characters like é, à, ö. Can this be do...
Formula to convert byte array representing signed integer into integer
This question is more generic without a particular language. I am more interested in solving this generally across languages. Every answer I find references a built-in method of something like getInt32 to extract an integer from a byte array. I have a byte array which contains the big-endian representation of a signed integer. Getting the values fo...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.