소프트웨어/c++

#define 속에 do while (0) 문장의 의미

개발자_이훈규 2015. 2. 12. 18:24




이 질문의 시작은 딱 이 문제이다.

http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro

( 구글 검색 결과 : https://www.google.co.kr/?gfe_rd=cr&ei=WINUU7XDCOK8iAelsYGYDQ#newwindow=1&safe=off&q=c%2B%2B+define+do+while+0 )

(예시)

#define TEST(x) do { \

    printf("%d\n", x); \

} while (0)



위 주소에서 얻을 수 있는 정보는

Macro는 정확히 할당이 되고, 따라서 { } 으로 묶으면 (scope이 생기기 때문에:제 생각) 안된다. 입니다.




마지막으로 이 예제를 보면 확실합니다.

http://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for