比如這樣一個問題:輸入一個正整數,將它轉為正整數。 可用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;
- }