PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

#! | 381 lines | 355 code | 26 blank | 0 comment | 0 complexity | 93618f410e74df5ab69858389bae1faa MD5 | raw file
  1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2 "">
  3<html xmlns="" xml:lang="en"
  4 lang="en" dir="ltr">
  6  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7  <title>data_types</title>
  8<meta name="generator" content="DokuWiki" />
  9<meta name="robots" content="index,follow" />
 10<meta name="date" content="2010-10-28T18:17:08-0700" />
 11<meta name="keywords" content="data_types" />
 12<link rel="search" type="application/opensearchdescription+xml" href="" title="C++ Reference" />
 13<link rel="start" href="../index.html" />
 14<link rel="contents" href="" title="Sitemap" />
 15<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="" />
 16<link rel="alternate" type="application/rss+xml" title="Current Namespace" href=";ns=" />
 17<link rel="alternate" type="text/html" title="Plain HTML" href="" />
 18<link rel="alternate" type="text/plain" title="Wiki Markup" href="" />
 19<link rel="canonical" href="data_types" />
 20<link rel="stylesheet" media="screen" type="text/css" href="lib/exe/css.php@t=custom1&amp;tseed=1289693594" />
 21<link rel="stylesheet" media="all" type="text/css" href="lib/exe/css.php@s=all&amp;t=custom1&amp;tseed=1289693594" />
 22<link rel="stylesheet" media="print" type="text/css" href="lib/exe/css.php@s=print&amp;t=custom1&amp;tseed=1289693594" />
 23<script type="text/javascript" ><!--//--><![CDATA[//><!--
 24var NS='';var JSINFO = {"id":"data_types","namespace":""};
 26<script type="text/javascript" charset="utf-8" src="lib/exe/js.php@tseed=1289693594" ></script>
 29<div class="dokuwiki export">
 30<!-- TOC START -->
 31<div class="toc">
 32<div class="tocheader toctoggle" id="toc__header">Table of Contents</div>
 33<div id="toc__inside">
 35<ul class="toc">
 36<li class="clear">
 38<ul class="toc">
 39<li class="level2"><div class="li"><span class="li"><a href="data_types#c_data_types" class="toc">C++ Data Types</a></span></div>
 40<ul class="toc">
 41<li class="level3"><div class="li"><span class="li"><a href="data_types#type_modifiers" class="toc">Type Modifiers</a></span></div></li>
 42<li class="level3"><div class="li"><span class="li"><a href="data_types#type_sizes_and_ranges" class="toc">Type Sizes and Ranges</a></span></div></li>
 43<li class="level3"><div class="li"><span class="li"><a href="data_types#reading_type_declarations" class="toc">Reading Type Declarations</a></span></div></li></ul>
 48<!-- TOC END -->
 50<h2 class="sectionedit1"><a name="c_data_types" id="c_data_types">C++ Data Types</a></h2>
 51<div class="level2">
 55C++ standard defines 7 basic types:
 58<div class="table sectionedit2"><table class="inline">
 59	<tr class="row0">
 60		<th class="col0">Type</th><th class="col1">Description</th>
 61	</tr>
 62	<tr class="row1">
 63		<td class="col0"><code>void</code></td><td class="col1">associated with no data type</td>
 64	</tr>
 65	<tr class="row2">
 66		<td class="col0"><code>bool</code></td><td class="col1">boolean value, true or false</td>
 67	</tr>
 68	<tr class="row3">
 69		<td class="col0"><code>char</code></td><td class="col1">character</td>
 70	</tr>
 71	<tr class="row4">
 72		<td class="col0"><code>int</code></td><td class="col1">integer</td>
 73	</tr>
 74	<tr class="row5">
 75		<td class="col0"><code>float</code></td><td class="col1">floating-point number</td>
 76	</tr>
 77	<tr class="row6">
 78		<td class="col0"><code>double</code></td><td class="col1">double precision floating-point number</td>
 79	</tr>
 80	<tr class="row7">
 81		<td class="col0"><code>wchar_t</code></td><td class="col1">wide character</td>
 82	</tr>
 84<!-- EDIT2 TABLE [64-316] -->
 86<!-- EDIT1 SECTION "C++ Data Types" [1-317] -->
 87<h3 class="sectionedit3"><a name="type_modifiers" id="type_modifiers">Type Modifiers</a></h3>
 88<div class="level3">
 92Several of these types can be modified using the keywords <code>signed</code>, <code>unsigned</code>, <code>short</code>, and <code>long</code>.  When one of these type modifiers is used by itself, a data type of <code>int</code> is assumed. A complete list of possible data types follows (equivalent types are displayed in the same row):
 95<div class="table sectionedit4"><table class="inline">
 96	<tr class="row0">
 97		<th class="col0" colspan="4">integer types</th>
 98	</tr>
 99	<tr class="row1">
100		<td class="col0" colspan="4"><code>bool</code></td>
101	</tr>
102	<tr class="row2">
103		<td class="col0" colspan="4"><code>char</code></td>
104	</tr>
105	<tr class="row3">
106		<td class="col0" colspan="4"><code>signed char</code></td>
107	</tr>
108	<tr class="row4">
109		<td class="col0" colspan="4"><code>unsigned char</code></td>
110	</tr>
111	<tr class="row5">
112		<td class="col0" colspan="4"><code>wchar_t</code></td>
113	</tr>
114	<tr class="row6">
115		<td class="col0"><code>short</code></td><td class="col1"><code>short int</code></td><td class="col2"><code>signed short</code></td><td class="col3"><code>signed short int</code></td>
116	</tr>
117	<tr class="row7">
118		<td class="col0" colspan="2"><code>unsigned short</code></td><td class="col2" colspan="2"><code>unsigned short int</code></td>
119	</tr>
120	<tr class="row8">
121		<td class="col0"><code>int</code></td><td class="col1"><code>signed</code></td><td class="col2" colspan="2"><code>signed int</code></td>
122	</tr>
123	<tr class="row9">
124		<td class="col0" colspan="2"><code>unsigned</code></td><td class="col2" colspan="2"><code>unsigned int</code></td>
125	</tr>
126	<tr class="row10">
127		<td class="col0"><code>long</code></td><td class="col1"><code>long int</code></td><td class="col2"><code>signed long</code></td><td class="col3"><code>signed long int</code></td>
128	</tr>
129	<tr class="row11">
130		<td class="col0" colspan="2"><code>unsigned long</code></td><td class="col2" colspan="2"><code>unsigned long int</code></td>
131	</tr>
132	<tr class="row12">
133		<th class="col0" colspan="4">floating point types</th>
134	</tr>
135	<tr class="row13">
136		<td class="col0" colspan="4"><code>float</code></td>
137	</tr>
138	<tr class="row14">
139		<td class="col0" colspan="4"><code>double</code></td>
140	</tr>
141	<tr class="row15">
142		<td class="col0" colspan="4"><code>long double</code></td>
143	</tr>
144	<tr class="row16">
145		<th class="col0" colspan="4">optionally supported integer types</th>
146	</tr>
147	<tr class="row17">
148		<td class="col0"><code>long long</code></td><td class="col1"><code>long long int</code></td><td class="col2"><code>signed long long</code></td><td class="col3"><code>signed long long int</code></td>
149	</tr>
150	<tr class="row18">
151		<td class="col0" colspan="2"><code>unsigned long long</code></td><td class="col2" colspan="2"><code>unsigned long long in</code>t</td>
152	</tr>
154<!-- EDIT4 TABLE [637-1281] -->
156<!-- EDIT3 SECTION "Type Modifiers" [318-1282] -->
157<h3 class="sectionedit5"><a name="type_sizes_and_ranges" id="type_sizes_and_ranges">Type Sizes and Ranges</a></h3>
158<div class="level3">
162The size and range of any data type is compiler and architecture dependent. You can use the <a href="keywords/sizeof" class="wikilink1" title="keywords:sizeof">sizeof</a> operator to determine the
163size of any data type (frequently expressed as a number of bytes). However, many architectures implement data types of a standard size. <code>ints</code> and <code>floats</code> are often 32-bit, chars 8-bit, and
164<code>doubles</code> are usually 64-bit. <code>bools</code> are often implemented as 8-bit data types.
165<code>long long</code> type is 64-bit. The “cfloat” (or “float.h”) header file defines the ranges for the floating types, the
166“climits” (or “limits.h”) - for the integer types.
170Limits for numeric values are defined in the &lt;limits&gt; header. The templated values of <a href="limits/numeric_limits" class="wikilink1" title="limits:numeric_limits">numeric_limits</a> provide system-dependant numerical representations of the C++ data types. Use the appropriate function given the data type as the template argument as shown in the table below. Note that numeric_limits can be overloaded for user-defined types as well.
173<div class="table sectionedit6"><table class="inline">
174	<tr class="row0">
175		<th class="col0"> Method or Constant </th><th class="col1"> Return </th><th class="col2"> Description </th>
176	</tr>
177	<tr class="row1">
178		<td class="col0">is_specialized</td><td class="col1">bool</td><td class="col2"> </td>
179	</tr>
180	<tr class="row2">
181		<td class="col0">radix</td><td class="col1">int</td><td class="col2">base of exponent</td>
182	</tr>
183	<tr class="row3">
184		<td class="col0">digits</td><td class="col1">int</td><td class="col2">number of radix digits in mantissa</td>
185	</tr>
186	<tr class="row4">
187		<td class="col0">digits10</td><td class="col1">int</td><td class="col2">number of base 10 digits in mantissa</td>
188	</tr>
189	<tr class="row5">
190		<td class="col0">is_signed</td><td class="col1">bool</td><td class="col2"> </td>
191	</tr>
192	<tr class="row6">
193		<td class="col0">is_integer</td><td class="col1">bool</td><td class="col2"> </td>
194	</tr>
195	<tr class="row7">
196		<td class="col0">is_exact</td><td class="col1">bool</td><td class="col2"> </td>
197	</tr>
198	<tr class="row8">
199		<td class="col0">min()</td><td class="col1">&lt;type&gt;</td><td class="col2">smallest number that can be respresented (not the most negative)</td>
200	</tr>
201	<tr class="row9">
202		<td class="col0">max()</td><td class="col1">&lt;type&gt;</td><td class="col2">largest number</td>
203	</tr>
204	<tr class="row10">
205		<td class="col0">epsilon()</td><td class="col1">&lt;type&gt;</td><td class="col2">inherent representation error value</td>
206	</tr>
207	<tr class="row11">
208		<td class="col0">round_error()</td><td class="col1">&lt;type&gt;</td><td class="col2">maximum rounding adjustment possible</td>
209	</tr>
210	<tr class="row12">
211		<td class="col0">infinity()</td><td class="col1">&lt;type&gt;</td><td class="col2"> </td>
212	</tr>
213	<tr class="row13">
214		<td class="col0">quiet_NaN()</td><td class="col1">&lt;type&gt;</td><td class="col2">invalid number that does not signal floating point error</td>
215	</tr>
216	<tr class="row14">
217		<td class="col0">signaling_NaN()</td><td class="col1">&lt;type&gt;</td><td class="col2">invalid number that signals floating point error</td>
218	</tr>
219	<tr class="row15">
220		<td class="col0">denorm_min()</td><td class="col1">&lt;type&gt;</td><td class="col2"> </td>
221	</tr>
222	<tr class="row16">
223		<td class="col0">min_exponent</td><td class="col1">int</td><td class="col2"> </td>
224	</tr>
225	<tr class="row17">
226		<td class="col0">min_exponent10</td><td class="col1">int</td><td class="col2"> </td>
227	</tr>
228	<tr class="row18">
229		<td class="col0">max_exponent</td><td class="col1">int</td><td class="col2"> </td>
230	</tr>
231	<tr class="row19">
232		<td class="col0">max_exponent10</td><td class="col1">int</td><td class="col2"> </td>
233	</tr>
234	<tr class="row20">
235		<td class="col0">has_infinity</td><td class="col1">bool</td><td class="col2"> </td>
236	</tr>
237	<tr class="row21">
238		<td class="col0">has_quiet_NaN</td><td class="col1">bool</td><td class="col2"> </td>
239	</tr>
240	<tr class="row22">
241		<td class="col0">has_signaling_NaN</td><td class="col1">bool</td><td class="col2"> </td>
242	</tr>
243	<tr class="row23">
244		<td class="col0">has_denorm</td><td class="col1">&lt;type&gt;_denorm_style</td><td class="col2"> </td>
245	</tr>
246	<tr class="row24">
247		<td class="col0">has_denorm_loss</td><td class="col1">bool</td><td class="col2"> </td>
248	</tr>
249	<tr class="row25">
250		<td class="col0">is_iec559</td><td class="col1">bool</td><td class="col2">conforms to IEC-559</td>
251	</tr>
252	<tr class="row26">
253		<td class="col0">is_bounded</td><td class="col1">bool</td><td class="col2"> </td>
254	</tr>
255	<tr class="row27">
256		<td class="col0">is_modulo</td><td class="col1">bool</td><td class="col2"> </td>
257	</tr>
258	<tr class="row28">
259		<td class="col0">traps</td><td class="col1">bool</td><td class="col2"> </td>
260	</tr>
261	<tr class="row29">
262		<td class="col0">tinyness_before</td><td class="col1">bool</td><td class="col2"> </td>
263	</tr>
264	<tr class="row30">
265		<td class="col0">round_style</td><td class="col1">float_round_style { round_to_nearest, … }</td><td class="col2"> </td>
266	</tr>
268<!-- EDIT6 TABLE [2266-3340] -->
271The most common usage is in bounds checking, to determine the minimum and maximum values a data type can hold. The following code prints out the minimum and maximum values for a short on the system it is run.
274<pre class="cpp code cpp">  <span class="co2">#include &lt;limits&gt;</span>
275  std<span class="sy4">::</span><span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;Maximum short value: &quot;</span> <span class="sy1">&lt;&lt;</span> std<span class="sy4">::</span><span class="me2">numeric_limits</span><span class="sy1">&lt;</span><span class="kw4">short</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">max</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">&lt;&lt;</span> std<span class="sy4">::</span><span class="me2">endl</span><span class="sy4">;</span>
276  std<span class="sy4">::</span><span class="kw3">cout</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;Minimum short value: &quot;</span> <span class="sy1">&lt;&lt;</span> std<span class="sy4">::</span><span class="me2">numeric_limits</span><span class="sy1">&lt;</span><span class="kw4">short</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">min</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy1">&lt;&lt;</span> std<span class="sy4">::</span><span class="me2">endl</span><span class="sy4">;</span></pre>
278<!-- EDIT5 SECTION "Type Sizes and Ranges" [1283-3766] -->
279<h3 class="sectionedit7"><a name="reading_type_declarations" id="reading_type_declarations">Reading Type Declarations</a></h3>
280<div class="level3">
284Simple type declarations are easy to understand:
286<pre class="cpp code cpp">  <span class="kw4">int</span> i</pre>
288However, it can be tricky to parse more complicated type declarations:
290<pre class="cpp code cpp">  <span class="kw4">double</span> <span class="sy2">**</span>d<span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span>              <span class="co1">// hmm...</span>
291  <span class="kw4">char</span> <span class="sy2">*</span><span class="br0">&#40;</span><span class="sy2">*</span><span class="br0">&#40;</span><span class="sy2">**</span>foo <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">8</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="co1">// augh! what is foo?</span></pre>
293To understand the above declarations, follow three rules:
297<li class="level1"><div class="li"> Start at the variable name (<code>d</code> or <code>foo</code> in the examples above)</div>
299<li class="level1"><div class="li"> End with the data type (<code>double</code> or <code>char</code> above)</div>
301<li class="level1"><div class="li"> Go right when you can, and left when you must.  (Grouping parentheses can cause you to bounce left.)</div>
307For example:
310<div class="table sectionedit8"><table class="inline">
311	<tr class="row0">
312		<th class="col0">Expression</th><th class="col1">Meaning</th>
313	</tr>
314	<tr class="row1">
315		<td class="col0"> <code>double **d[8];</code> </td><td class="col1"> </td>
316	</tr>
317	<tr class="row2">
318		<td class="col0"> <code><del>double</del> **<del>d</del>[8];</code> </td><td class="col1"> <strong>d is … double</strong> </td>
319	</tr>
320	<tr class="row3">
321		<td class="col0"> <code><del>double</del> **<del>d[8]</del>;</code> </td><td class="col1"> d is <strong>an array of 8</strong> … double </td>
322	</tr>
323	<tr class="row4">
324		<td class="col0"> <code><del>double</del> *<del>*d[8]</del>;</code> </td><td class="col1"> d is an array of 8 <strong>pointer to</strong> … double </td>
325	</tr>
326	<tr class="row5">
327		<td class="col0"> <code><del>double **d[8]</del>;</code> </td><td class="col1"> d is an array of 8 pointer to <strong>pointer to</strong> double </td>
328	</tr>
330<!-- EDIT8 TABLE [4363-4752] -->
333Another example:
336<div class="table sectionedit9"><table class="inline">
337	<tr class="row0">
338		<th class="col0">Expression</th><th class="col1">Meaning</th>
339	</tr>
340	<tr class="row1">
341		<td class="col0"> <code>char *(*(**foo [][8])())[]</code> </td><td class="col1"> </td>
342	</tr>
343	<tr class="row2">
344		<td class="col0"> <code><del>char</del> *(*(**<del>foo</del> [][8])())[]</code> </td><td class="col1"> <strong>foo is … char</strong> </td>
345	</tr>
346	<tr class="row3">
347		<td class="col0"> <code><del>char</del> *(*(**<del>foo []</del>[8])())[]</code> </td><td class="col1"> foo is <strong>an array of</strong> … char </td>
348	</tr>
349	<tr class="row4">
350		<td class="col0"> <code><del>char</del> *(*(**<del>foo [][8]</del>)())[]</code> </td><td class="col1"> foo is an array of <strong>an array of 8</strong> … char </td>
351	</tr>
352	<tr class="row5">
353		<td class="col0"> <code><del>char</del> *(*(*<del>*foo [][8]</del>)())[]</code> </td><td class="col1"> foo is an array of an array of 8 <strong>pointer to</strong> … char </td>
354	</tr>
355	<tr class="row6">
356		<td class="col0"> <code><del>char</del> *(*<del>(**foo [][8])</del>())[]</code> </td><td class="col1"> foo is an array of an array of 8 pointer to <strong>pointer to</strong> … char </td>
357	</tr>
358	<tr class="row7">
359		<td class="col0"> <code><del>char</del> *(*<del>(**foo [][8])()</del>)[]</code> </td><td class="col1"> foo is an array of an array of 8 pointer to pointer to <strong>function returning</strong> … char </td>
360	</tr>
361	<tr class="row8">
362		<td class="col0"> <code><del>char</del> *<del>(*(**foo [][8])())</del>[]</code> </td><td class="col1"> foo is an array of an array of 8 pointer to pointer to function returning <strong>pointer to</strong> … char </td>
363	</tr>
364	<tr class="row9">
365		<td class="col0"> <code><del>char</del> *<del>(*(**foo [][8])())[]</del></code> </td><td class="col1"> foo is an array of an array of 8 pointer to pointer to function returning pointer to <strong>array of</strong> … char </td>
366	</tr>
367	<tr class="row10">
368		<td class="col0"> <code><del>char *(*(**foo [][8])())[]</del></code> </td><td class="col1"> foo is an array of an array of 8 pointer to pointer to function returning pointer to array of <strong>pointer to</strong> char </td>
369	</tr>
371<!-- EDIT9 TABLE [4772-6014] -->
374For a much more detailed explanation, see Steve Friedl&#039;s excellent description of how to read C declarations at <a href="" class="urlextern" title=""  rel="nofollow"></a>.
379<!-- EDIT7 SECTION "Reading Type Declarations" [3767-] --></div>