아래소스좀 봐주세요.
글쓴이: jee89 / 작성시간: 토, 2002/12/28 - 8:46오전
int a = 7777;
int b = 8888;
int c = 9999;
char str[20];
sprintf( str , "%2d%2d%2d" , a , b , c );
printf( "%s" , str );
-------------------
결과 777788889999
와 같이 나오는데
포맷스트링을 %2.2d%2.2d%2.2d로 바꾸어도 마찬가지이군요.
결과가 778899 이렇게 나오고 하고 싶은데
어떻게 하면 되죠?
(2자리초과하는 정수에서 앞2자리만 필요할때입니다.)
Forums:


int a = 7777; int b = 8888; int c =
int a = 7777;
int b = 8888;
int c = 9999;
char str[20];
sprintf( str1 , "%d" , a );
sprintf( str2 , "%d" , b );
sprintf( str3 , "%d" , c );
후에, 각 str[1-3]의 앞의 2글자를 추출해서, strcat으로 합치면 되지 않을까요?
DTSTTCPW
int a = 7777; int b = 8888; int c =
int a = 7777;
int b = 8888;
int c = 9999;
char str[20];
sprintf( str , "%d%d%d" , (a%100) , (b%100) , (c%100) );
printf( "%s" , str );
==
하시면 될꺼 같은데요...
음 그냥 심심해서 짜봤습니다.
/* * two_num.cc */ #include <unistd.h> #include <stdio.h> #include <string.h> int get_2num(int num, char *str, int len) { if (!str || num < 10) { return -1; } memset(str, 0, len); snprintf(str, len - 1, "%d", num); if (str + 2) { *(str + 2) = '\0'; } return 0; } int main() { int a = 7700; int b = 8800; int c = 9900; char buf[1024]; char str[20]; memset(str, 0, sizeof str); if (get_2num(a, buf, sizeof buf) != -1) { strncat(str, buf, sizeof str - 1); } if (get_2num(b, buf, sizeof buf) != -1) { strncat(str, buf, sizeof str - 1); } if (get_2num(c, buf, sizeof buf) != -1) { strncat(str, buf, sizeof str - 1); } printf("%d %d %d\n" "%s\n", a, b, c, str); }댓글 달기