summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/macros.html
blob: b7726cf5e1561c8d0891cd7382bac6675afe3e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<title>Macros</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Macros</h1>
</center>
<hr>
<p>
Macros are built on the <a href="define_preprocessor.html">#define</a> 
preprocessor.<p>
Normally a #define would look like:
<pre>
	#define PI 3.142
</pre>
But, a macro would look like this.
<pre>
	#define SQUARE(x) x*x
</pre>
The main difference is that the first example is a constant and the second 
is an expression. 
If the <b>macro</b> above was used in some code it may look like this:
<pre>
        #define SQUARE(x)  x*x

        main()
        {
          int value=3;
          printf("%d \n", SQUARE(value));
        }
</pre>
After preprocessing the code would become:
<pre>
        main()
        {
          int value=3;
          printf("%d \n", value*value);
        }
</pre>
<hr>
<h2>Examples:</h2>

<a href="../EXAMPLES/macro.c"><img src="../../GRAPHICS/computer.gif"></a>
macro example.<p>
<hr>
<h2>Notes:</h2>
<ul>
<li>The value passed to SQUARE could have been an 
<a href="../CONCEPT/data_types.html#int">int</a>
<a href="../CONCEPT/data_types.html#float">float</a>
or
<a href="../CONCEPT/data_types.html#double">double</a>
<li>Long macros can span multiple lines by using a 
<a href="../FUNCTIONS/escape.html">\</a> followed by a newline
(return).
<li>If you are using gcc to compile your code, you can use the -E option to
see the preprocessor output 
</ul>
<hr>
<h2>See Also:</h2>
<ul>
<li><A href="define_preprocessor.html">#define</a> preprocessor.
<font color=brown>
<li><A href="../../CPLUSPLUS/SYNTAX/inline.html">inline</a> A C++ keyword that provides a better aproch to macros.
</font>
</ul>


<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>