#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temporary_number_s
{
char buffer[32];
char offset;
char length;
} temporary_number_t;
char * formatNumber(long long num)
{
static temporary_number_t source, destination;
int is_comma_position=0;
int commas=0;
int i=0;
memset(&destination, 0, sizeof(temporary_number_t));
/* -999 ~ 999 */
if (num > -1000 && num < 1000 )
{
sprintf(destination.buffer, "%ld", num);
return destination.buffer;
}
memset(&source, 0, sizeof(temporary_number_t));
sprintf(source.buffer, "%ld", num);
source.length = strlen(source.buffer); /* source.l is bigger than 3 */
if(num<0)
commas=(source.length-2)/3; /* exclude '-' character */
else
commas=(source.length-1)/3;
destination.length = source.length + commas;
destination.offset= destination.length-1;
source.offset = source.length-1;
is_comma_position=0;
while (source.offset>-1)
{
destination.buffer[destination.offset] = source.buffer[source.offset];
destination.offset--;
source.offset--;
is_comma_position++;
if (is_comma_position == 3 && commas)
{
commas--;
destination.buffer[destination.offset] = ',';
destination.offset--;
is_comma_position = 0;
}
}
return destination.buffer;
}
int main(int argc, char **argv)
{
printf("%s ... %s\n", argv[1], formatNumber(atol(argv[1])));
return 0;
}
'Dev Tool Tips/C/C++'에 해당되는 글 26건
- 2013.08.16 Number to comma separated number string
- 2012.01.03 BYTE SWAP 매크로
- 2011.12.01 [C/C++ TIP] 특정 문자열로 시작하는지 판단하는 매크로
- 2011.11.13 __attribute ((constructor))와 ((destructor))
- 2011.05.13 Makefile로 빌드할 때마다 version 명이 포함되도록 헤더 파일 생성하기
- 2010.05.06 C Tip: 사용하지 않는 함수의 파라미터에 대한 매크로 처리
- 2010.04.05 unsigned long 정수의 printf
- 2010.04.01 생각해볼 코드 조각
- 2009.12.01 임베디드 리눅스에서 마운트되어 있는 USB 미디어 갯수를 세는 프로그램 만들기
- 2009.11.10 리눅스 개발환경 라이브러리 생성 예
main() 함수보다 먼저 시작하고 메인 함수가 끝난 뒤에도 실행되는 함수
/* --- test.c START --- */
#include <stdio.h>
void hello(void) __attribute__ ((constructor)) ;
void goodbye(void) __attribute__ ((destructor)) ;
void hello(void)
{
printf("Hello\n");
}
void goodbye(void)
{
printf("BYE\n");
}
int main(int argc, char** argv)
{
printf("==== main === \n");
return 0;
}
/* --- test.c END --- */
$ cc test.c
$ ./a.out
Hello
==== main ===
BYE
rm -f $@
@echo "creating $@ ..."; \
version=`cat ./MODEL_NAME`; \
today=`date +"%Y.%m.%d" `; \
time=`date +"%H:%M:%S" `; \
echo "#ifndef VERSION_H_INCLUDED" > $@; \
echo "#define VERSION_H_INCLUDED" >> $@; \
echo "#define MY_VERSION \"$$version ($$today, $$time)\"" >> $@; \
echo "#endif /* VERSION_H_INCLUDED */" >> $@
함수를 만들 때는 필요할 것 같았는데 막상 어떤 이유에서건 사용하지 않는 파라미터가 생기는 경우가 있다.
물론 함수를 호출하는 쪽에서나 정의한 곳, 선언한 곳 모두 찾아서 지우면 되긴 하지만
다음에도 쓸 가능성이 있고 해서 남겨둘 필요가 있을 때는 다음과 같이 매크로를 사용하면 편리하다.
이렇게 해두면 컴파일러에서 사용되지 않는 파라미터에 대한 경고가 나오는 것도 막을 수 있다.
#define PARAM_UNSED(x) (void)x)
int test_function(int foo)
{
#if 0
return foo+2;
#else
PARAM_UNSED(foo);
return 0;
참고로 Microsoft Visual Studio에서는 UNREFERENCED_PARAMETER라는 매크로로 다음과 같이 winnt.h에 정의되어 있다.
#define UNREFERENCED_PARAMETER(P) (P)
#define DBG_UNREFERENCED_PARAMETER(P) (P)
#define DBG_UNREFERENCED_LOCAL_VARIABLE(V) (V)
/* test.c */
1 #include <stdio.h>
2
3 int main(int argc, char** argv)
4 {
5 int a[10]={0,1,2,3,4,5,6,7,8,9};
6 printf(" a: %p\n", a);
7 printf(" &a: %p\n", &a);
8 printf(" a+1: %p\n", a+1);
9 printf("&a+1: %p\n", &a+1);
10 return 0;
11 }
$ cc test.c
$ ./a.out
a: 0x7fff15795fc0
&a: 0x7fff15795fc0
a+1: 0x7fff15795fc4
&a+1: 0x7fff15795fe8
감이 오는가?
$ gcc -c test.c
$ ar rc libtest.a test.o
공유라이브러리
$ gcc -fPIC -c test.c
$ gcc -shared -W1,-soname,libtest.so.1 -o libtest.so.1.0.1 test.o
$ cp libtest.so.1.0.1 /usr/local/lib
$ ln -s /usr/local/lib/libtest.so.1.0.1 /usr/local/lib/libtest.so
공유라이버리 사용
$ gcc -o mysoftware mysoftware.c -L/usr/local/lib -l
------ 아래는 공유라이브러리 사용 예 ------
/*-------------------shared_test.h-------------------*/
int lib_test(void);
/*-------------------shared_test.c-------------------*/
#include <stdio.h>
#include "shared_test.h"
int g_counter=0;
int lib_test(void)
{
int ret=++g_counter;
return ret;
}
/*-------------------test1.c-------------------*/
#include <stdio.h>
#include "shared_test.h"
int main(void)
{
sleep(5);
while(1)
{
printf("TEST1: %d\n", lib_test());
sleep(5);
}
return 0;
}
/*-------------------test2.c-------------------*/
#include <stdio.h>
#include "shared_test.h"
int main(void)
{
sleep(5);
while(1)
{
printf("TEST2: %d\n", lib_test());
sleep(5);
}
return 0;
}
------------- MAKEFILE -----------------
all: clean
mkdir -p obj
gcc -fPIC -c shared_test.c -o obj/shared_test.o
gcc -shared -W1,-soname,libtest.so.1 -o obj/libtest.so obj/shared_test.o
gcc -o obj/test1 test1.c -L./obj -ltest
gcc -o obj/test2 test2.c -L./obj -ltest
clean:
rm -rf obj/*