마음의 안정을 찾기 위하여 - 가변 인자를 사용하는 매크로 만들기
2266960
1297
804
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
가변 인자를 사용하는 매크로 만들기
리눅스/리눅스 프로그래밍 | 2010/05/06 12:10
출처 : http://www.comeaucomputing.com/techtalk/c99/#variadicmacros

가변인자를 사용하는 매크로 함수를 만드는 방법...

Support for Variadic macros is now available. Consider:

#include <stdio.h>

// In C89/90, you need multiple macros with "funny" names to do this.
// Anyway, in this example, the arguments passed (those represented
// by the ...) replace the __VA_ARGS__ when the substitution occurs.
#define DoLogFile(...) fprintf(stderr, __VA_ARGS__)

// Variadic macros also help out if you want to effectively
// pass one argument  that contains commas
#define MakeName(...)  #__VA_ARGS__

// Of course, the macro can also take other args, so long as
// the ... is last
#define output(FILEptr, ...) fprintf(FILEptr, __VA_ARGS__)

// This is not right, since this is NOT a variadic macro
#define mac(arg) foo(arg, __VA_ARGS__)

int main()
{
    int bound = 99;
    int current_index = 88;
    int linenumber = 77;

    DoLogFile("Detected array out of bounds\n");
    DoLogFile("Bound = %d, current index =%d\n", bound, current_index);
    DoLogFile("Missing semicolon on line %d\n", linenumber);
    output(stderr, "Detected array out of bounds\n");

    char *p;
    p = MakeName(Mary);
    p = MakeName(Doe, John);
    p = MakeName(Martin Luther King, Jr.);
    // Combo two variadic macros
    DoLogFile("MakeName=%s\n", MakeName(This is a test, yes it is));
}


가변인자를 사용하는 매크로 함수를 이용하면, 손쉽게 디버깅용 함수를 만들어 사용할 수 있다.

2010/05/06 12:10 2010/05/06 12:10
Article tag list Go to top
View Comment 1
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
From. 구차니 2010/05/06 16:54
답글달기삭제
후덜덜 멋지십니다 -ㅁ-!
전 그냥

#define debug_print() printf("%s %d\n",__func__,__LINE__)
을 사랑합니다 ㅋ
PREV : [1] : NEXT
 
 
 
 
: [1] ... [263][264][265][266][267][268][269][270][271] ... [1317] :
«   2024/03   »
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
전체 (1317)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (5)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (6)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)