Open In App

How to Convert an Integer to a String in C?

Last Updated : 16 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to convert the given integer value to string.

Examples

Input: 1234
Output: "1234"
Explanation: The integer 1234 is converted to the string "1234".

Input: -567
Output: "-567"
Explanation: The integer -567 is converted to the string "-567".

Different Methods to Convert an Integer to a String in C

Following are the three methods of converting the integers to string in C:

1. Manual Conversion Using Loop

We can also convert an integer to string manually by using by extracting each digit, converting it to its corresponding character and storing it in the array.

Below is the approach for this idea:

  • Extract the digit using modulo operator.
  • Convert the single digit number to corresponding character by adding it to '0' character.
  • Remove the last digit using division operator.

Implementation

C
// C Program to Manually Convert an Integer to a String
// using loops
#include <stdio.h>
#include <string.h>

void intToStr(int N, char *str) {
    int i = 0;
  
    // Save the copy of the number for sign
    int sign = N;

    // If the number is negative, make it positive
    if (N < 0)
        N = -N;

    // Extract digits from the number and add them to the
    // string
    while (N > 0) {
      
        // Convert integer digit to character and store
      	// it in the str
        str[i++] = N % 10 + '0';
      	N /= 10;
    } 

    // If the number was negative, add a minus sign to the
    // string
    if (sign < 0) {
        str[i++] = '-';
    }

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

    // Reverse the string to get the correct order
    for (int j = 0, k = i - 1; j < k; j++, k--) {
        char temp = str[j];
        str[j] = str[k];
        str[k] = temp;
    }
}

int main() {
    int N = 1234;
    char str[12];

    intToStr(N, str);

    printf("String: %s\n", str);

    return 0;
}

Output
String: 1234

Time Complexity: O (log10N), where N is the number.
Auxiliary Space: O (log10N)

2. Using sprintf() Function

To convert an integer to a string, we can use the sprintf function in C. This function prints/outputs a formatted string in a string buffer.

Syntax of sprintf()

sprintf(buffer, formatted_string, input_args);

By specifying the %d format specifier in the formatted string and providing the integer as argument, we can store the number as a string in the given buffer.

Implementation

The below program demonstrates how we can convert an integer to a string in C.

C
// C Program to Convert an Integer to a String
#include <stdio.h>

int main() {
  
    // Integer to be converted
    int N = 86;

    // Buffer to hold the resulting string
    char str[20];

    // Converting integer to string using sprintf
    sprintf(str, "%d", N);

    printf("The integer %d converted to string is: %s\n",
           N, str);

    return 0;
}

Output
The integer 86 converted to string is: 86

Time Complexity: O (log10N), where N is the number. 
Auxiliary Space: O (log10N)

Note: We can also use the snprintf function which is similar to sprintf but with a buffer size limit, which helps prevent buffer overflows.

3. Using itoa() Function

itoa() is a non-standard function available in some C compilers like MSVC, etc. It stands for Integer TO ASCII. It can be used to convert the given integer to string.

Syntax of itoa()

itoa(integer, string, base)

Implementation

C
// C Program to illustrate how to convert a number to string using itoa() function
#include <stdio.h>
#include <stdlib.h>

int main() {
    int N = 1234;
  
    // Declare a character array 'str' t store the converted
    // string
    char str[12];

    // Calling itoa()
    itoa(N, str, 10);

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


Output

String: 1234

Time Complexity: O (log10N), where N is the number.
Auxiliary Space: O (log10N)


Next Article

Similar Reads