Manual Conversion Using Loop
One way to convert an integer to a string is by extracting each digit using loops and storing it in a character array.
#include <stdio.h>
#include <string.h>
void intToStr(int num, char str[]) {
int i = 0, temp, len = 0;
temp = num;
// Find number of digits
while (temp != 0) {
len++;
temp /= 10;
}
str[len] = '\0'; // Null-terminate the string
// Extract digits in reverse order
while (num != 0) {
str[--len] = (num % 10) + '0';
num /= 10;
}
}
int main() {
int number = 4567;
char str[20];
intToStr(number, str);
printf("String representation: %s\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
String representation: 4567
Explanation
- We find the number of digits in the integer.
- We extract each digit and store it in a character array.
- We use the ASCII value of '0' ('0' has a value of 48) to convert numbers to characters.
- The resulting string is null-terminated for proper handling.
Using sprintf() Function
The sprintf() function is a standard C function that formats and stores data in a character array.
#include <stdio.h>
int main() {
int number = 7890;
char str[20];
sprintf(str, "%d", number);
printf("String representation: %s\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
String representation: 7890
Explanation
- The sprintf() function works like printf() but stores the result in a character array instead of printing it on the screen.
- It automatically handles conversion, making it a simple and effective method.
Using itoa() Function
The itoa() function is a non-standard function available in some C libraries (like <stdlib.h> in some compilers). It converts an integer to a string.
#include <stdio.h>
#include <stdlib.h>
int main() {
int number = 12345;
char str[20];
itoa(number, str, 10); // Convert integer to string with base 10
printf("String representation: %s\n", str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
String representation: 12345
Explanation
- itoa() converts the integer to a string.
- The third argument (10) specifies the base (decimal in this case).
- This method is easy but not part of the C standard, meaning it may not be available on all compilers.
C Program to Convert Integer to String Using Custom Conversion Function
Converting an integer to a string in C requires a custom function because C doesn’t provide a built-in function for this specific task. The idea is to break down the integer digit by digit, convert each digit to its character equivalent, & store it in a string. Let’s write a complete C program to achieve this.
The code is :
include <stdio.h>
include <string.h>
// Function to reverse a string
void reverseString(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
// Function to convert integer to string
void intToString(int num, char str[]) {
int i = 0;
int isNegative = 0;
// Handle negative numbers
if (num < 0) {
isNegative = 1;
num = -num;
}
// Convert each digit to character
do {
str[i++] = num % 10 + '0'; // Convert digit to character
num = num / 10;
} while (num > 0);
// Add negative sign if needed
if (isNegative) {
str[i++] = '-';
}
str[i] = '\0'; // Null-terminate the string
// Reverse the string to get the correct order
reverseString(str);
}
int main() {
int number = -12345;
char result[20]; // Array to store the resulting string
intToString(number, result); // Call the conversion function
printf("Integer: %d\n", number);
printf("String: %s\n", result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this Code:
1. Reverse String Function:
- The `reverseString` function reverses the string. This is necessary because we extract digits from the integer in reverse order (from least significant digit to most significant digit).
- For example, if the integer is `123`, the digits are extracted as `3`, `2`, `1`. Reversing this gives us the correct string `"123"`.
2. Integer to String Function:
- The `intToString` function handles the conversion.
- It first checks if the number is negative. If it is, it sets a flag & converts the number to positive for easier processing.
- It then extracts each digit using the modulo operator (`%`) & converts it to a character by adding `'0'` (which is the ASCII value of `0`).
- The digits are stored in the string array in reverse order.
- After all digits are processed, the function adds a negative sign if the original number was negative & null-terminates the string.
- Finally, the string is reversed to get the correct order.
3. Main Function:
- The `main` function demonstrates how to use the `intToString` function.
- It initializes an integer, calls the conversion function, & prints both the integer & the resulting string.
Runtime Test Cases
To ensure our program works correctly, let’s test it with different types of integers. Runtime test cases help us verify the functionality of the code under various scenarios. Below are some test cases & their expected outputs.
Test Case 1: Positive Integer
Input:
int number = 12345;
Expected Output:
Integer: 12345
String: 12345
Test Case 2: Negative Integer
Input:
int number = -6789;
Expected Output:
Integer: -6789
String: -6789
Test Case 3: Zero
Input:
int number = 0;
Expected Output:
Integer: 0
String: 0
Test Case 4: Large Positive Integer
Input:
int number = 2147483647; // Maximum value for a 32-bit signed integer
Expected Output:
Integer: 2147483647
String: 2147483647
Test Case 5: Large Negative Integer
Input:
int number = -2147483648; // Minimum value for a 32-bit signed integer
Expected Output:
Integer: -2147483648
String: -2147483648
Running the Test Cases
To run these test cases, you can modify the `main` function in the previous code as follows:
int main() {
int testCases[] = {12345, -6789, 0, 2147483647, -2147483648};
char result[20];
for (int i = 0; i < 5; i++) {
intToString(testCases[i], result);
printf("Integer: %d\n", testCases[i]);
printf("String: %s\n\n", result);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Explanation of Test Cases
1. Positive Integer: Tests the basic functionality of the program with a standard positive number.
2. Negative Integer: Ensures the program correctly handles negative numbers by adding a `-` sign.
3. Zero: Verifies that the program works for the edge case of zero.
4. Large Positive Integer: Checks if the program can handle the maximum value for a 32-bit signed integer.
5. Large Negative Integer: Tests the program with the minimum value for a 32-bit signed integer.
Frequently Asked Questions
What is the best way to convert an integer to a string in C?
The sprintf() function is the best option because it is easy to use, reliable, and available in all C libraries.
Why should I avoid itoa() for integer-to-string conversion?
itoa() is not a standard C function and may not be available on all compilers, making it less portable.
Can we use snprintf() instead of sprintf()?
Yes, snprintf() is a safer version of sprintf() as it prevents buffer overflow by limiting the number of characters written to the string.
Conclusion
In this article, we discussed different ways to convert an integer to a string in C. The most commonly used methods include sprintf(), itoa(), and snprintf(), each offering different levels of control over formatting and memory usage. Choosing the right approach depends on factors like portability and efficiency. Understanding these methods helps in handling numeric-to-string conversions effectively in C programming.