|
|
1.1 root 1:
2:
3: #define Preprocessing Directive #define
4:
5:
6:
7:
8: Define an identifier as a macro
9:
10: #ddeeffiinnee _i_d_e_n_t_i_f_i_e_r _l_p_a_r_e_n _i_d_e_n_t_i_f_i_e_r-_l_i_s_t9_o_p_t8 ) _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
11:
12: The preprocessing directive #define tells the C preprocessor to
13: regard identifier as a macro.
14:
15: #define can define two kinds of macros: object-like, and
16: function-like.
17:
18: An object-like macro has the syntax
19:
20:
21: #define _i_d_e_n_t_i_f_i_e_r _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
22:
23:
24: This type of macro is also called a manifest constant. The
25: preprocessor searches for identifier throughout the text of the
26: translation unit, and replaces it with the elements of
27: replacement-list, which is then rescanned for further macro sub-
28: stitutions.
29:
30: For example, consider the directive:
31:
32:
33: #define BUFFERSIZE 75
34:
35:
36: When the preprocessor reads the line
37:
38:
39: malloc(BUFFERSIZE);
40:
41:
42: it replaces it with:
43:
44:
45: malloc(75);
46:
47:
48: A given identifier is replaced only once by a given replacement-
49: list. This is to prevent such code as
50:
51:
52: #define FOO FOO
53:
54:
55: or
56:
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: #define Preprocessing Directive #define
70:
71:
72:
73: #define FOO BAR
74: #define BAR FOO
75:
76:
77: from generating an infinite loop.
78:
79: A function-like macro is more complex. It has the syntax:
80:
81:
82: #define _i_d_e_n_t_i_f_i_e_r _l_p_a_r_e_n _i_d_e_n_t_i_f_i_e_r-_l_i_s_t9_o_p_t8 ) _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
83:
84:
85: The preprocessor looks for identifier, which is a macro that
86: resembles a function in that it is followed by a pair of paren-
87: theses that may enclose an identifier-list. It replaces iden-
88: tifier with the contents of replacement-list, up to the first
89: lparen `(' within replacement-list.
90:
91: The preprocessor then examines identifier-list for further mac-
92: ros, which it expands. The modified identifier-list is then
93: replaced with the rest of replacement-list. Pairs of parentheses
94: that are nested between the lparen that begins replacement-list
95: and the `)' that ends it are copied into identifier-list as
96: literal characters. The identifiers within identifier-list are
97: preserved after it has been modified by replacement-list. The
98: only exceptions are identifiers that are prefixed by the
99: preprocessing operators # or ##; these are handled appropriately.
100:
101: For example, the consider the macro:
102:
103:
104: #define display(x) show((long)(x), #x)
105:
106:
107: When the preprocessor reads the following line
108:
109:
110: display(abs(-5));
111:
112:
113: it replaces it with the following:
114:
115:
116: show((long)(abs(-5)), "abs(-5)");
117:
118:
119: When an argument to a function-like macro contains no preproces-
120: sing tokens, or when an argument to a function-like macro con-
121: tains a preprocessing token that is identical to a preprocessing
122: directive, the behavior is undefined.
123:
124: ***** Example *****
125:
126: For an example of using a function-like macro in a program, see
127: #.
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: #define Preprocessing Directive #define
136:
137:
138:
139:
140: ***** See Also *****
141:
142: #, ##, #undef, C preprocessor
143:
144: ***** Notes *****
145:
146: A macro expansion always occupies exactly one line, no matter how
147: many lines are spanned by the definition or the actual
148: parameters. If you have defined macros that span more than one
149: line, you must either redefine them to occupy one line, or some-
150: how embed the newline character within the macro itself; other-
151: wise, the macro will not expand correctly.
152:
153: A macro definition can extend over more than one line, provided
154: that a backslash `\' appears before the newline character that
155: breaks the lines. The size of a #define directive is therefore
156: limited by the maximum size of a logical source line, which can
157: be up to at least 509 characters long.
158:
159: Some implementations allowed a user to re-define a macro with a
160: new #define directive. The Standard, however, allows only a
161: ``benign'' redefinition; that is, the body of the new definition
162: must exactly match the old definition, including parameter names
163: and white space.
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.