1<html lang="en"> 2<head> 3<title>Function-like Macros - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Macros.html#Macros" title="Macros"> 9<link rel="prev" href="Object_002dlike-Macros.html#Object_002dlike-Macros" title="Object-like Macros"> 10<link rel="next" href="Macro-Arguments.html#Macro-Arguments" title="Macro Arguments"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Function-like-Macros"></a> 54<a name="Function_002dlike-Macros"></a> 55<p> 56Next: <a rel="next" accesskey="n" href="Macro-Arguments.html#Macro-Arguments">Macro Arguments</a>, 57Previous: <a rel="previous" accesskey="p" href="Object_002dlike-Macros.html#Object_002dlike-Macros">Object-like Macros</a>, 58Up: <a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a> 59<hr> 60</div> 61 62<h3 class="section">3.2 Function-like Macros</h3> 63 64<p><a name="index-function_002dlike-macros-45"></a> 65You can also define macros whose use looks like a function call. These 66are called <dfn>function-like macros</dfn>. To define a function-like macro, 67you use the same ‘<samp><span class="samp">#define</span></samp>’ directive, but you put a pair of 68parentheses immediately after the macro name. For example, 69 70<pre class="smallexample"> #define lang_init() c_init() 71 lang_init() 72 ==> c_init() 73</pre> 74 <p>A function-like macro is only expanded if its name appears with a pair 75of parentheses after it. If you write just the name, it is left alone. 76This can be useful when you have a function and a macro of the same 77name, and you wish to use the function sometimes. 78 79<pre class="smallexample"> extern void foo(void); 80 #define foo() /* <span class="roman">optimized inline version</span> */ 81 ... 82 foo(); 83 funcptr = foo; 84</pre> 85 <p>Here the call to <code>foo()</code> will use the macro, but the function 86pointer will get the address of the real function. If the macro were to 87be expanded, it would cause a syntax error. 88 89 <p>If you put spaces between the macro name and the parentheses in the 90macro definition, that does not define a function-like macro, it defines 91an object-like macro whose expansion happens to begin with a pair of 92parentheses. 93 94<pre class="smallexample"> #define lang_init () c_init() 95 lang_init() 96 ==> () c_init()() 97</pre> 98 <p>The first two pairs of parentheses in this expansion come from the 99macro. The third is the pair that was originally after the macro 100invocation. Since <code>lang_init</code> is an object-like macro, it does not 101consume those parentheses. 102 103 </body></html> 104 105