Development

#define ๋งคํฌ๋กœ ์‚ฌ์šฉ๋ฒ•

JayKim๐Ÿ™‚ 2023. 6. 9. 09:25

์šฐ๋ฆฌ๋ผ C/C++ ๊ฐœ๋ฐœ ํ•˜๋ฉด Define ๋งคํฌ๋กœ๋ฅผ ์ž์ฃผ ์ ‘ํ•˜๊ฒŒ ๋œ๋‹ค.
์‚ฌ์‹ค Define ๋งคํฌ๋กœ๊ฐ€ ์†Œ์Šค๋ฅผ ๋ณด๊ธฐ ๋ฐ ๋””๋ฒ„๊น…์— ์–ด๋ ค์šด ์ ์„ ์ œ๊ณต ํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ
๊ฐœ๋ฐœ์˜ ๊ฐ€๋…์„ฑ ๋ฐ ๋‹จ์ˆœ ๋ฐ˜๋ณต์„ ์ค„์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต ํ•œ๋‹ค.

๊ทธ๋Ÿผ define ๋งคํฌ๋กœ๋Š” ํ•œ๋งˆ๋””๋กœ ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž ๊ทธ๋ฆฌ๊ณ  ๊ธฐํ˜ธ์— ๋Œ€ํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž๋กœ ์น˜ํ™˜์„ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

#define ์ง€์ •๋ฌธ์ž ๋Œ€์ฒด๋ฌธ์ž
  • Define ์˜ˆ์ œ
#define ADD(A,B) A + B
printf( "Define ADD(A,B) : %d, ADD(1,2));

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค
์ฆ‰ ํ™”๋ฉด์˜ ๊ฐ’์€
Define ADD(A,B) : 3 ์„ ์ถœ๋ ฅ ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค.
๊ฐ€๋” define ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์†Œ์Šค ์ดํ•ด๊ฐ€ ์–ด๋ ค์šธ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋•Œ๋Š”
์†Œ์Šค๋ฅผ ์ดํ•ด ํ• ๋•Œ define ๊ฐ’์„ ๋Œ€์ฒด๋œ ์†Œ์Šค ๊ฐ’์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค( ๋‹น์—ฐํ•œ ๋ง์ธ๊ฐ€ ^^; )

๊ทธ๋ฆฌ๊ณ  define ๋งคํฌ๋กœ์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๋งคํฌ๋กœ ์—ฐ์‚ฐ์ž 2๊ฐ€์ง€ ์žˆ๋Š”๋ฐ # ๊ณผ ## ์ด ๋‘๊ฐœ์˜ ๋ฌธ์ž์ด๋‹ค.

  • # : ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๋Š” ์—ฐ์‚ฐ์ž
  • ## : ํ† ํฐ ๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž

์ด ๋‘๊ฐ€์ง€๊ฐ€ ์“ฐ์ธ๋‹ค.
๊ทธ๋Ÿผ ์˜ˆ์ œ๋ฅผ ๋ณด๊ตฌ ๊ฐ’์„ ์ดํ•ดํ•ด ๋ณด์ž

# ๋ฌธ์ž์—ด ์˜ˆ์ œ

#define Test(n) printf( #n "\n" )

Test(3)

์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•˜๋ฉด
3
๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ฆ‰ #n ์€ ๋ฌธ์ž์—ด "3"์„ ๋œปํ•˜๊ฒŒ ๋œ๋‹ค.

## ๋ฌธ์ž์—ด ์˜ˆ์ œ

#define TOKEN_TEST(n) printf( "token" #n " = %d\n", token ## n )
int token7 = 10;

printf( "token" "7" " = %d\n", token7 );
TOKEN_TEST(7);

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋œ token ##n ์€ token7 ์ด๋ผ๋Š” ## n ์ด ์ž…๋ ฅ ๊ฐ’ 7 ๋กœ ๋Œ€์ฒด ๋˜์–ด์„œ ๋ฌธ์ž๊ฐ€ ์—ฐ๊ฒฐ ๋œ token7 ๋กœ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
๋ฌธ์ž์—ด ๋Œ€์ฒด๊ฐ€ ์•„๋‹Œ ์ฝ”๋“œ์˜ ์—ฐ๊ฒฐ ๋œ ๊ฐ’์ธ ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ณผ ๋ฉ”์„ธ์ง€

token7 = 10
token7 = 10
  • Define ์˜ค๋ฅ˜ ๊ฒฝ์šฐ
#include <stdio.h>
#define MUL\_A(A,B) A \* B  
#define MUL\_B(A,B) (A) \* (B)

int main( int argc, char \*argv\[\] )  
{  
int nX = 10;  
int nY = 20;


// nX(10) + 1 * nY(20) + 2 = 10 + 1 * 20 + 2 = 32
printf( "Define MUL_A : %d * %d = %d\n", nX+1, nY+2, MUL_A( nX+1, nY+2 ) );

// (nX + 1) * (nY + 2) = ( 10 + 1 ) * ( 20 + 2 ) = 242
printf( "Define MUL_B : %d * %d = %d\n", nX+1, nY+2, MUL_B( nX+1, nY+2 ) );

return 0;
}
  • ๊ฒฐ๊ณผ ํ™”๋ฉด
Define MUL_A : 11 * 22 = 32  
Define MUL_B : 11 * 22 = 242

์ด๋ ‡๊ฒŒ ๊ฐ™์€ ๋ณ€์ˆ˜ ์ฒ˜๋ฆฌ์— ๋”ฐ๋ผ ๊ฐ’์ด ๋‹ฌ๋ผ์ง„๋‹ค.
์ฆ‰ MUL_B ๋ฐฉ์‹ ์ฒ˜๋Ÿผ ๊ด„ํ˜ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ฃผ์–ด์•ผ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

์กฐ๊ฑด ์ฒ˜๋ฆฌ๋ฌธ

#if, #ifdef, #ifndef, #else, #elif, #endif

๋ณต์ˆ˜ ์ฒ˜๋ฆฌ

#ifdef _CONDITION1
#ifdef _CONDITION2
_CONDITION ๊ณผ CONDITION2 ๋‘๊ฐœ ๋ชจ๋‘ ํ•„์š”ํ•จ
#endif
#endif


- AND ๊ฒฝ์šฐ
#if defined _CONDITION1 && defined _CONDITION
_CONDITION1 ๊ณผ _CONDITION2 ๋ชจ๋‘ ์ •์˜ ํ•„์š”
#endif

#if !defined _CONDITION && !defined _CONDITION2
_CONDITION1 ๊ณผ _CONDITION2 ๋ชจ๋‘ ์ •์˜๊ฐ€ ์•ˆ๋˜์–ด์•ผ ํ•จ
#endif

- OR ๊ฒฝ์šฐ
#if defined _CONDITION || defined _CONDITION2
_CONDITION1 ๊ณผ _CONDITION2 ๋‘๊ฐœ์ค‘ 1๊ฐœ ์ด์ƒ ์ •์˜ ํ•„์š”ํ•จ
#endif

#if !defined _CONDITION1 || !defined _CONDITION2
_CONDITION1 ๊ณผ _CONDITION2 ๋‘๊ฐœ์ค‘ 1๊ฐœ ์ด์ƒ ์ •์˜๊ฐ€ ์•ˆ๋˜์–ด์•ผ ํ•จ
#endif