0
0
CHow-ToBeginner · 2 min read

How to Convert Integer to String in C with Example

In C, you can convert an integer to a string using sprintf(buffer, "%d", number); where buffer is a char array to hold the string and number is the integer.
📋

Examples

Input123
Output"123"
Input0
Output"0"
Input-456
Output"-456"
🧠

How to Think About It

To convert an integer to a string in C, you need to create a character array to store the result. Then, use a function that formats the integer as text and writes it into that array. This way, the number becomes a readable string you can print or use elsewhere.
📐

Algorithm

1
Create a character array (buffer) to hold the string.
2
Use a formatting function to write the integer into the buffer as text.
3
Use or print the buffer as the string representation of the integer.
💻

Code

c
#include <stdio.h>

int main() {
    int number = 123;
    char str[12]; // enough for int including sign and null
    sprintf(str, "%d", number);
    printf("Converted string: %s\n", str);
    return 0;
}
Output
Converted string: 123
🔍

Dry Run

Let's trace converting the integer 123 to a string.

1

Initialize variables

number = 123, str = uninitialized char array

2

Call sprintf

sprintf(str, "%d", 123) writes "123" into str

3

Print result

printf prints "Converted string: 123"

StepVariableValue
1number123
2str"123"
3outputConverted string: 123
💡

Why This Works

Step 1: Buffer for string

We create a char array to hold the string because C strings are arrays of characters ending with a null character.

Step 2: Using sprintf

sprintf formats the integer into text and stores it in the buffer, converting the number to its string form.

Step 3: Printing the string

We print the buffer with printf using %s to show the string version of the integer.

🔄

Alternative Approaches

Using snprintf
c
#include <stdio.h>

int main() {
    int number = 456;
    char str[12];
    snprintf(str, sizeof(str), "%d", number);
    printf("Converted string: %s\n", str);
    return 0;
}
snprintf is safer than sprintf because it limits the number of characters written to avoid buffer overflow.
Using itoa (non-standard)
c
#include <stdlib.h>
#include <stdio.h>

int main() {
    int number = -789;
    char str[12];
    itoa(number, str, 10); // base 10
    printf("Converted string: %s\n", str);
    return 0;
}
itoa is a simpler function but is not part of the C standard and may not be available on all systems.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of digits in the integer, so it is O(n) where n is the digit count.

Space Complexity

Space is needed for the output string buffer, proportional to the number of digits plus one for the null terminator.

Which Approach is Fastest?

Using sprintf or snprintf is efficient and standard; itoa may be faster but less portable.

ApproachTimeSpaceBest For
sprintfO(n)O(n)Standard, simple conversion
snprintfO(n)O(n)Safe conversion with buffer limit
itoaO(n)O(n)Quick conversion but non-standard
💡
Always ensure your char array is large enough to hold the integer string plus the null character.
⚠️
Forgetting to allocate enough space in the char array can cause buffer overflow and crashes.