Convert an Integer to a String in C

In C, we can convert an integer to a string using functions like sprintf(), itoa(), or manual conversion using character manipulation.


Examples of Integer to String Conversion

1. Using sprintf() Function

In this example, we will use sprintf() to convert an integer into a string and store it in a character array.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 1234;
    char str[10];  // Buffer to store the converted string

    // Convert integer to string using sprintf
    sprintf(str, "%d", num);

    printf("Converted String: %s\n", str);
    return 0;
}

Explanation:

  1. We declare an integer variable num and assign it a value of 1234.
  2. We create a character array str[10] to hold the converted string.
  3. We use sprintf() to convert num to a string and store it in str. The format specifier %d ensures the integer is correctly formatted.
  4. We print the converted string using printf().

Output:

Converted String: 1234

2. Using itoa() Function to Convert Integer to String (Non-standard)

In this example, we will use the itoa() function to convert an integer to a string. Note that itoa() is not part of the standard C library but is available in many compilers.

main.c

</>
Copy
#include <stdio.h>
#include <stdlib.h> // Required for itoa()

int main() {
    int num = -5678;
    char str[10];  // Buffer to store the converted string

    // Convert integer to string using itoa
    itoa(num, str, 10);  // 10 specifies base 10 (decimal)

    printf("Converted String: %s\n", str);
    return 0;
}

Explanation:

  1. We declare an integer variable num and assign it a value of -5678.
  2. We create a character array str[10] to store the converted string.
  3. We use itoa() to convert num into a string and store it in str. The third parameter 10 specifies the base (decimal).
  4. We print the converted string using printf().

Output:

Converted String: -5678

3. Manual Conversion Using Character Manipulation

In this example, we will manually convert an integer to a string by extracting its digits and storing them in a character array.

main.c

</>
Copy
#include <stdio.h>

void intToStr(int num, char str[]) {
    int i = 0, isNegative = 0;

    // Handle negative numbers
    if (num < 0) {
        isNegative = 1;
        num = -num;
    }

    // Extract digits in reverse order
    do {
        str[i++] = (num % 10) + '0';
        num /= 10;
    } while (num > 0);

    // Add negative sign if needed
    if (isNegative) {
        str[i++] = '-';
    }

    str[i] = '\0';  // Null-terminate the string

    // Reverse the string
    int start = 0, end = i - 1;
    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

int main() {
    int num = -9012;
    char str[10];  // Buffer to store the converted string

    intToStr(num, str);
    printf("Converted String: %s\n", str);
    return 0;
}

Explanation:

  1. We define a function intToStr() to convert an integer to a string.
  2. We check if the number is negative, store its sign, and convert it to a positive number.
  3. We extract digits one by one in reverse order and store them as characters in str.
  4. We add a null character ('\0') to mark the end of the string.
  5. We reverse the string to restore the correct order.
  6. In main(), we call intToStr() and print the converted string.

Output:

Converted String: -9012

Conclusion

In this tutorial, we explored different methods to convert an integer to a string in C:

  1. sprintf(): Simple and widely supported method.
  2. itoa(): Useful but not a standard function.
  3. Manual Conversion: A flexible approach using character manipulation.