1<html lang="en"> 2<head> 3<title>Typeof - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Constructing-Calls.html#Constructing-Calls" title="Constructing Calls"> 10<link rel="next" href="Conditionals.html#Conditionals" title="Conditionals"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Typeof"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Conditionals.html#Conditionals">Conditionals</a>, 53Previous: <a rel="previous" accesskey="p" href="Constructing-Calls.html#Constructing-Calls">Constructing Calls</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.6 Referring to a Type with <code>typeof</code></h3> 59 60<p><a name="index-typeof-2260"></a><a name="index-sizeof-2261"></a><a name="index-macros_002c-types-of-arguments-2262"></a> 61Another way to refer to the type of an expression is with <code>typeof</code>. 62The syntax of using of this keyword looks like <code>sizeof</code>, but the 63construct acts semantically like a type name defined with <code>typedef</code>. 64 65 <p>There are two ways of writing the argument to <code>typeof</code>: with an 66expression or with a type. Here is an example with an expression: 67 68<pre class="smallexample"> typeof (x[0](1)) 69</pre> 70 <p class="noindent">This assumes that <code>x</code> is an array of pointers to functions; 71the type described is that of the values of the functions. 72 73 <p>Here is an example with a typename as the argument: 74 75<pre class="smallexample"> typeof (int *) 76</pre> 77 <p class="noindent">Here the type described is that of pointers to <code>int</code>. 78 79 <p>If you are writing a header file that must work when included in ISO C 80programs, write <code>__typeof__</code> instead of <code>typeof</code>. 81See <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>. 82 83 <p>A <code>typeof</code>-construct can be used anywhere a typedef name could be 84used. For example, you can use it in a declaration, in a cast, or inside 85of <code>sizeof</code> or <code>typeof</code>. 86 87 <p>The operand of <code>typeof</code> is evaluated for its side effects if and 88only if it is an expression of variably modified type or the name of 89such a type. 90 91 <p><code>typeof</code> is often useful in conjunction with the 92statements-within-expressions feature. Here is how the two together can 93be used to define a safe “maximum” macro that operates on any 94arithmetic type and evaluates each of its arguments exactly once: 95 96<pre class="smallexample"> #define max(a,b) \ 97 ({ typeof (a) _a = (a); \ 98 typeof (b) _b = (b); \ 99 _a > _b ? _a : _b; }) 100</pre> 101 <p><a name="index-underscores-in-variables-in-macros-2263"></a><a name="index-g_t_0040samp_007b_005f_007d-in-variables-in-macros-2264"></a><a name="index-local-variables-in-macros-2265"></a><a name="index-variables_002c-local_002c-in-macros-2266"></a><a name="index-macros_002c-local-variables-in-2267"></a> 102The reason for using names that start with underscores for the local 103variables is to avoid conflicts with variable names that occur within the 104expressions that are substituted for <code>a</code> and <code>b</code>. Eventually we 105hope to design a new form of declaration syntax that allows you to declare 106variables whose scopes start only after their initializers; this will be a 107more reliable way to prevent such conflicts. 108 109<p class="noindent">Some more examples of the use of <code>typeof</code>: 110 111 <ul> 112<li>This declares <code>y</code> with the type of what <code>x</code> points to. 113 114 <pre class="smallexample"> typeof (*x) y; 115</pre> 116 <li>This declares <code>y</code> as an array of such values. 117 118 <pre class="smallexample"> typeof (*x) y[4]; 119</pre> 120 <li>This declares <code>y</code> as an array of pointers to characters: 121 122 <pre class="smallexample"> typeof (typeof (char *)[4]) y; 123</pre> 124 <p class="noindent">It is equivalent to the following traditional C declaration: 125 126 <pre class="smallexample"> char *y[4]; 127</pre> 128 <p>To see the meaning of the declaration using <code>typeof</code>, and why it 129might be a useful way to write, rewrite it with these macros: 130 131 <pre class="smallexample"> #define pointer(T) typeof(T *) 132 #define array(T, N) typeof(T [N]) 133</pre> 134 <p class="noindent">Now the declaration can be rewritten this way: 135 136 <pre class="smallexample"> array (pointer (char), 4) y; 137</pre> 138 <p class="noindent">Thus, <code>array (pointer (char), 4)</code> is the type of arrays of 4 139pointers to <code>char</code>. 140</ul> 141 142 <p><em>Compatibility Note:</em> In addition to <code>typeof</code>, GCC 2 supported 143a more limited extension which permitted one to write 144 145<pre class="smallexample"> typedef <var>T</var> = <var>expr</var>; 146</pre> 147 <p class="noindent">with the effect of declaring <var>T</var> to have the type of the expression 148<var>expr</var>. This extension does not work with GCC 3 (versions between 1493.0 and 3.2 will crash; 3.2.1 and later give an error). Code which 150relies on it should be rewritten to use <code>typeof</code>: 151 152<pre class="smallexample"> typedef typeof(<var>expr</var>) <var>T</var>; 153</pre> 154 <p class="noindent">This will work with all versions of GCC. 155 156 </body></html> 157 158