[CTAP] 6# The Preprocessor

문연수·2022년 8월 20일
0

CTAP

목록 보기
7/9

1. Words

  • contiguous: 인접한, 근접한
    a. a sharing a common border; toucing.
    b. a next or together in sequence.
  • collating: present participle of collate
  • collate:
    1. collection and combine (texts, information, or data)
     a. compare and analyse (two or more sources of information)
     b. verify the number and order of (the sheets of a book).
    - (여러 출처에서 정보를) 수집, 분석하다.
    - (종이나 페이지를) 순서대로 모으다[맞추다]
  • hazard:
    1. a danger or risk.
    2. a potential source of danger.
    3. say (something) in a temtative way
    4. put (something) at risk of being lost.
    - 위험 요소
    - ~을 위태롭게 하다
    - 틀릴 셈치고[모함삼아] 제안[추측]하다.
  • tempt:
    1. (좋지 않은 일을 하도록) 유혹하다[부추기다].
    2. (어떤 것을 제의하거나 하여) 유도[설득]하다.
  • tempting: 솔깃한, 구미가 당기는.
  • conceptually: 개념적으로.

이젠 좀 외우자!

2. Summery

- 1. Space matter in macro definitions

 Defining macros is a little trickier than calling them. For instance, does the definition of f in

#define f (x) ((x) - 1)

represents

(x) ((x) - 1)

but, does not apply to macro calls. just to macro definitions.

 Thus after the last definition above, f(3) and f (3) both evaluate to 2.

- 2. Macros are not functions.

  1. Evaluating arguments more than once is dangerous
    #define max(a, b) ((a) > (b) ? (a) : (b))
    /* i can be increased twice */
    biggest = max(biggest, n[i++]);
  2. The nested macro call can be larger than expected.
    max(a, max(b, max(c, d)))
    /* will be expanded to */
     ((a)>(((b)>(((c)>(d)?(c):(d)))?(b):(((c)>(d)?(c):(d)))))?
      (a):(((b)>(((c)>(d)?(c):(d)))?(b):(((c)>(d)?(c):(d))))))

- 3. Macros are not statements

#define assert(e) if (!e) assert_error(__FILE__, __LINE__)

if (x > 0 && y > 0)
	assert (x > y);
else
	assert (y > x);    

 will cause dangling-else problem. The right way to define assert() is: make the body of assert() look like an expression and not a statement:

#define assert(e)	\
		((void) ((e) || _assert_error(__FILE__, __LINE__)))

 책에는 소개되지 않았으나 do ~ while(0) 을 통해 compound statement 를 안전하게 작성할 수 있다:

#define assert(e)									\
		do {										\
   	    	if (!e)									\
            	assert_error(__FILE__, __LINE__);	\
        } while (0) 

- 4. Macros are not type definitions

#define FOOTYPE struct foo
FOOTYPE a;
FOOTYPE b, c;

Using a macro definition for this has the advantage of portability - any C compiler supports it. But it is better to use a type definition:

typedef struct foo FOOTYPE;

Consider the following example:

#define T1 struct foo *
typedef struct foo *T2;

T1 a, b;
/* is equal to
  struct foo *a, b;
*/
T2 c, d;
/* is equal to
   struct foo *c, *d;
*/

3. Exercise

https://www.mythos-git.com/Cruzer-S/CTAP/-/tree/main/Chapter06

4. References

[Book] C Traps and Pitfalls (Andrew Koenig)
[Site] 네이버 영영, 영한사전
[Site] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

profile
2000.11.30

0개의 댓글