๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Problem Solving/C++ ์ •๋ฆฌ

C++ #4: preprocessor directives

by ํ–‰๋ฑ 2019. 12. 7.

preprocessor directives

preprocessor๋Š” ์ผ๋ฐ˜ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ธฐ ์ „์— preprocessor directives๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

preprocessor directives๋Š” ์ „์ฒ˜๋ฆฌ๊ธฐ ์ง€์‹œ์ž๋ผ๊ณ  ํ•˜๋ฉฐ,

'#'์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ ๊ฐœํ–‰๋ฌธ์ž๋กœ ๋๋‚œ๋‹ค. (์„ธ๋ฏธ์ฝœ๋ก  ๋ถˆํ•„์š”, ๊ฐœํ–‰ ์ง์ „ '\'์„ ์“ฐ๋ฉด ๋‹ค์Œ ์ค„์— ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ)

 

๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์“ฐ์ผ ์ˆ˜ ์žˆ์œผ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” ์ž์ฃผ ์“ฐ์ด๋Š” ์„ธ ๊ฐ€์ง€ ์šฉ๋„๋งŒ ์†Œ๊ฐœํ•œ๋‹ค.

 

 

1) Source file inclusion (#include)

#include <header>
#includ "file"

preprocessor๋Š” #include ์ง€์‹œ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด

๋’ค์— ๋ช…์‹œ๋œ ํ—ค๋”๋‚˜ ํŒŒ์ผ์„ ์ง€์‹œ์ž ์œ„์น˜์— ๋ณต์‚ฌํ•ด ๋Œ€์ฒดํ•œ๋‹ค.

 

๋‘ ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ,

implementation ์‹œ ์ปดํŒŒ์ผ๋Ÿฌ์™€ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ํ—ค๋”๋ฅผ incluldeํ•  ๋•Œ๋Š” <>,

์†Œ์Šค ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ผ์„ includeํ•  ๋•Œ๋Š” ""์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ file์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ""๋ฅผ <>๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

 

 

2) Macro definitions (#define, #undef)

#define identifier replacement

preprocessor๊ฐ€ #define ์ง€์‹œ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด

๋’ค์— ๋‚˜์˜ค๋Š” ์ฝ”๋“œ์—์„œ identifier๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค replacement๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

 

#define TABLE_SIZE 10
int table1[TABLE_SIZE];
int table2[TABLE_SIZE];

#define getmax(a, b) a > b ? a : b

ํ˜•ํƒœ๋Š” ๋™์ผํ•˜์ง€๋งŒ ๋‘ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ,

๋งˆ์น˜ ์ƒ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  (์ด ๊ฒฝ์šฐ ๋Œ€์ฒด๋กœ identifier๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐ)

๋งˆ์น˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (function macro๋ผ๊ณ  ํ•จ)

 

function macro์—์„œ๋Š” replacement์— ๋‘ ๊ฐ€์ง€ ํŠน๋ณ„ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ,

# ์—ฐ์‚ฐ์ž๋Š” ๋ฐ”๋กœ ๋’ค์— ์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ŠคํŠธ๋ง ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉฐ

## ์—ฐ์‚ฐ์ž๋Š” ๋‘ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ (๊ณต๋ฐฑ ์—†์ด) ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

#define str(x) #x
cout << str(test);         // cout << "test";

#defie glue(a, b) a ## b
glue(c, out) << "test";    // cout << "test";

 

#define ์ง€์‹œ์ž๋Š” #undef ์ง€์‹œ์ž๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ์ง€์†๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,

#define TABLE_SIZE 10
int table1[TABLE_SIZE];    // int table1[10];
#undef TABLE_SIZE
#define TABLE_SIZE 20
int table2[TABLE_SIZE];    // int table2[20];

 

๋‹จ, ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋„ˆ๋ฌด ๋งคํฌ๋กœ์— ์˜์กดํ•˜์ง€ ์•Š๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

3) Conditional inclusions (#ifdef, #ifndef, #if, #elif, #else, #endif)

preprocessor๊ฐ€ ์ด ์ง€์‹œ์ž๋“ค์„ ๋งŒ๋‚˜๋ฉด

์–ด๋–ค ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ (์ปดํŒŒ์ผ ๋Œ€์ƒ์—) ํฌํ•จํ•  ์ง€ ํฌํ•จํ•˜์ง€ ์•Š์„ ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

#ifdef TABLE_SIZE
int table[TABLE_SIZE];
#endif

#ifndef TABLE_SIZE
#define TABLE_SIZE 10
#endif
int table[TABLE_SIZE];

#ifdef ์ง€์‹œ์ž๋Š” ๋’ค์— ๋‚˜์˜ค๋Š” identifier๊ฐ€ ์•ž์—์„œ ์ด๋ฏธ ์ •์˜๋˜์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. (๊ฐ’๊ณผ๋Š” ๊ด€๊ณ„ ์—†์Œ)

์ฐธ์ธ ๊ฒฝ์šฐ #endif ์ง€์‹œ์ž๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „ ๊นŒ์ง€์˜ ์ฝ”๋“œ๊ฐ€ ์ปดํŒŒ์ผ ๋˜๋„๋ก ํ•œ๋‹ค.

#ifndef ์ง€์‹œ์ž๋Š” #ifdef ์ง€์‹œ์ž์™€ ๋ฐ˜๋Œ€๋กœ ๋™์ž‘ํ•œ๋‹ค.

 

#if TABLE_SIZE > 20
#undef TABLE_SIZE
#define TABLE_SIZE 20

#elif TABLE_SIZE < 15
#undef TABLE_SIZE
#define TABLE_SIZE 15

#else
#undef TABLE_SIZE
#define TABLE_SIZE 10
#endif

#if, #elif, #else ์ง€์‹œ์ž๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ identifier์˜ ๊ฐ’๊นŒ์ง€ ํ™•์ธํ•œ๋‹ค.

๋”ฐ๋ผ ๋‚˜์˜ค๋Š” ์กฐ๊ฑด๋ฌธ์€ constant expressions์™€ macro expressions๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

#if ์ง€์‹œ์ž๋Š”

ํŠน๋ณ„ ์—ฐ์‚ฐ์ž defined์™€ !defined๋ฅผ ์ด์šฉํ•ด

#ifdef, #ifndef ์ง€์‹œ์ž์˜ ํ–‰๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ฐจ์ด์ ์€ #ifdef ์ง€์‹œ์ž๋กœ๋Š” ํ•˜๋‚˜์˜ ์กฐ๊ฑด๋งŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,

#if ์ง€์‹œ์ž + defined ์—ฐ์‚ฐ์ž๋กœ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (&&, || ์ด์šฉ)

 

References

http://www.cplusplus.com/doc/tutorial/preprocessor/

http://sarghis.com/blog/802/

https://boycoding.tistory.com/145

'Problem Solving > C++ ์ •๋ฆฌ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

C++ ์ˆœ์ฐจ ์ปจํ…Œ์ด๋„ˆ (vector, deque, list)  (0) 2020.05.31
C++14 STL ์ฒ ์ € ์ž…๋ฌธ) vector  (0) 2020.05.20
C++ #3: call by value, address, reference  (0) 2019.12.06
C++ #2: reference variable  (0) 2019.12.05
C++ #1: iostream, namespace  (0) 2019.12.05

๋Œ“๊ธ€