C 언어에서 정수를 문자열로 변환하는 예

예를 들어, 다음과 같은 문제가 있습니다. 양의 정수를 입력하고이를 양의 정수로 변환합니다. sprintf 및 snprint로 구현할 수 있습니다. 코드 쇼 :

#include <stdio.h>
#include <string.h>

int a=0;
char buffer[8];

int main() {

	do {
		printf("input an unsigned int:");
		scanf("%u", &a);
	} while(a<0);

	snprintf(buffer, sizeof(buffer)+1, "%d", a);

	printf("length of buffer:%d, value is:%s\n ", strlen(buffer), buffer);

	return 0;
}


 

추천

출처blog.csdn.net/huzhenwei/article/details/6941627