코딩을 하다보면, 문자열에..

숫자를 넣어야 할 때가 있다..

그럴 때 유용한 것이 바로 sprintf 이다..

sprintf를 쓰기 위해서는

#include <stdio.h> 가 필요하다...

사용법은...

char buf[256] ;

int a = 9 ;

sprintf( buf , " test%d", a ) ;

이렇게 넣으면..

buf 에는 test9 라는 문자열이 저장된다...

응용하면,

char *string = "world" ;
sprintf( buf, "Hello %s %d", string, a ) ;

이렇게 쓰면,

buf에 Hello world 9 라고 저장된다...

또,

char buf[256] ;

for( int i = 0 ; i < 3 ; ++i )
{
sprinf( buf, "test %d", i ) ;
printf( "%s\n", buf ) ;
}

이렇게 쓰면,

test0
test1
test2

이런식으로 출력된다..
Posted by 바람처럼..
|