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