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:
- We declare an integer variable
numand assign it a value of1234. - We create a character array
str[10]to hold the converted string. - We use
sprintf()to convertnumto a string and store it instr. The format specifier%densures the integer is correctly formatted. - 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:
- We declare an integer variable
numand assign it a value of-5678. - We create a character array
str[10]to store the converted string. - We use
itoa()to convertnuminto a string and store it instr. The third parameter10specifies the base (decimal). - 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:
- We define a function
intToStr()to convert an integer to a string. - We check if the number is negative, store its sign, and convert it to a positive number.
- We extract digits one by one in reverse order and store them as characters in
str. - We add a null character (
'\0') to mark the end of the string. - We reverse the string to restore the correct order.
- In
main(), we callintToStr()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:
sprintf(): Simple and widely supported method.itoa(): Useful but not a standard function.- Manual Conversion: A flexible approach using character manipulation.
