Software Engineering

How one can Convert an Integer to a String in C

Written by admin


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

Choice 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 major(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;
}

Choice 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 major(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;
}

About the author

admin

Leave a Comment