How to Convert an Integer to a String in C?
Last Updated :
16 Aug, 2024
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;
}
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;
}
OutputThe 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)
Related Articles:
Similar Reads
How to Convert a String to a Char Array in C?
In C, the only difference between the string and a character array is possibly the null character '\0' but strings can also be declared as character pointer in which case, its characters are immutable. In this article, we will learn how to convert a string to a char array in C. The most straightforw
2 min read
Convert String to int in C
In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language. Example: Input: "1234"Output: 1234Explanation
6 min read
Convert string to integer without using any in-built functions
Given a string str, the task is to convert the given string into the number without using any inbuilt function. Examples: Input: str = "985632" Output: 985632 Explanation: Given input is in string form and returned output is in integer form. Input: str = "123" Output: 123 Explanation: Given input is
11 min read
How to Append a Character to a String in C?
In this article, we will learn how to append a character to a string using the C program. The most straightforward method to append a character to a string is by using a loop traverse to the end of the string and append the new character manually. [GFGTABS] C #include <stdio.h> void addChar(ch
3 min read
Converting String to Long in C
Here, we will see how to build a C Program For String to Long Conversion using strtol() function. Syntax: long int strtol(char *string, char **ptr, int base)The first argument is given as a stringThe second argument is a reference to an object of type char*The third argument denotes the base in whic
4 min read
How to Reverse a String in C?
In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu
2 min read
How to Match a Pattern in a String in C?
Matching a pattern in a string involves searching for a specific sequence of characters within a larger string. In this article, we will learn different methods to efficiently match patterns in a string in C. The most straightforward method to match a string is by using strstr() function. Letâs take
3 min read
How to Split a String by a Delimiter in C?
Splitting a string by a delimiter is a common task in programming. In C, strings are arrays of characters, and there are several ways to split them based on a delimiter. In this article, we will explore different methods to split a string by a delimiter in C. Splitting Strings in CThe strtok() metho
2 min read
Array of Pointers to Strings in C
In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
2 min read
C Program to Concatenate Two Strings Using a Pointer
Concatenating two strings means appending one string at the end of another string. While the standard library provides strcat() for concatenation, this article will demonstrate how to concatenate two strings using pointers. To concatenate two strings using pointers, traverse the first string to its
1 min read