How to Convert an Integer to a String in C

0 min read 114 words

If you need to convert an Integer to a String in C, then you can do one of the following:

Option 1 – Use sprintf()

int sprintf(char *str, const char *format, [arg1, arg2, ... ]);

You can do something like this:

#include <stdio.h>

int main(void) {
	int number;
	char text[20]; 

	printf("Enter a number: ");
	scanf("%d", &number);

	sprintf(text, "%d", number);

	printf("\nYou have entered: %s", text);

	return 0;
}

Option 2 – Use itoa()

char* itoa(int num, char * buffer, int base)

You can do something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(void) {
    int number,l;
    char string[20];
    
    printf("Enter a number: ");
    scanf("%d", &number);
    
    itoa(number,string,10);
    
    printf("String value = %s\n", string);
    
    return 0;
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags