Tips on how to Convert an Integer to a String in C

If you should convert an Integer to a String in C, then you are able to do one of many following:

Possibility 1 – Use sprintf()#

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

You are able to do one thing like this:

#embody <stdio.h>

int fundamental(void) {
	int quantity;
	char textual content[20]; 

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

	sprintf(textual content, "%d", quantity);

	printf("nYou have entered: %s", textual content);

	return 0;
}

Possibility 2 – Use itoa()#

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

You are able to do one thing like this:

#embody <stdio.h>
#embody <stdlib.h>
#embody <string.h>
 
int fundamental(void) {
    int quantity,l;
    char string[20];
    
    printf("Enter a quantity: ");
    scanf("%d", &quantity);
    
    itoa(quantity,string,10);
    
    printf("String worth = %sn", string);
    
    return 0;
}