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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
<title>C Data types</title>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>C Data types.</h1>
</center>
<hr>
<p>
<h2>Variable definition</h2>
C has a concept of '<i>data types</i>' which are used to
<a href="../glossary.html#definition">define</a> a variable
before its use. <p>
The definition of a variable will assign storage for the variable and
define the type of data that will be held in the location.<p>
So what data types are available?
<p>
<table border=1 bgcolor=ivory width=80%>
<tr align=center>
<td><a href="#int">int</a>
<td><a href="#float">float</a>
<td><a href="#double">double</a>
<td><a href="#char">char</a>
<td><A HREF="../SYNTAX/void.html">void</A>
<td><A HREF="../SYNTAX/enum.html">enum</A>
</table>
<p>
Please note that there is not a boolean data type. C does not have the
traditional view about logical comparison, but thats another story.<p>
<font color="brown">
Recent C++ compilers do have a <a href="../../CPLUSPLUS/SYNTAX/bool.html">boolean</a> datatype.
<p>
</font>
<hr>
<h2><a name="int">int - data type</h2>
<b>int</b> is used to define integer numbers.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>
{
int Count;
Count = 5;
}
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="float">float - data type</h2>
<b>float</b> is used to define floating point numbers.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>
{
float Miles;
Miles = 5.6;
}
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="double">double - data type</h2>
<b>double</b> is used to define BIG floating point numbers. It reserves twice
the storage for the number. On PCs this is likely to be 8 bytes.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>
{
double Atoms;
Atoms = 2500000;
}
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="char">char - data type</h2>
<b>char</b> defines characters.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>
{
char Letter;
Letter = 'x';
}
</pre>
</td></tr></table>
</center>
<p>
<a name="modifier">
<hr>
<h2>Modifiers</h2>
The three data types above have the following modifiers.
<p>
<ul>
<li>short
<li>long
<li>signed
<li>unsigned
</ul>
The modifiers define the amount of storage allocated to the variable.
The amount of storage allocated is not cast in stone. ANSI has the
following rules:<p>
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>
short int <= int <= long int
float <= double <= long double
</pre>
</td></tr></table>
</center>
<p>
What this means is that a 'short int' should assign less than or the same
amount of storage as an 'int' and the 'int' should be less or the same bytes
than a 'long int'. What this means in the real world is:
<p>
<table border=2 width="100%" bgcolor="ivory">
<tr><td>
<pre>
Type Bytes Bits Range
</pre>
</td></tr>
<tr><td>
<pre>
short int 2 16 -16,384 -> +16,383 (16kb)
unsigned short int 2 16 0 -> +32,767 (32Kb)
unsigned int 4 16 0 -> +4,294,967,295 ( 4Gb)
int 4 32 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
long int 4 32 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
signed char 1 8 -128 -> +127
unsigned char 1 8 0 -> +255
float 4 32
double 8 64
long double 12 96
</pre>
</td></tr></table>
<p>
These figures only apply to todays generation of PCs. Mainframes and
midrange machines could use different figures, but would still comply
with the rule above.<p>
You can find out how much storage is allocated to a data type by using
the <a href="../SYNTAX/sizeof.html">sizeof</a> operator.
<a name="qualifier">
<hr>
<h2>Qualifiers</h2>
<p>
<ul>
<li><a href="../SYNTAX/const.html">const</a>
<li><a href="../SYNTAX/volatile.html">volatile</a>
</ul>
The <i>const</i> qualifier is used to tell C that the variable value can not
change after initialisation.<p>
const float pi=3.14159;
<p>
<i>pi</i> cannot be changed at a later time within the program.<p>
Another way to define constants is with the
<a href="../SYNTAX/define_preprocessor.html">#define</a> preprocessor which
has the advantage that it does not use any storage (but who counts bytes
these days?).<p>
<hr>
<h2>See also:</h2>
<a href="type_conv.html">Data type conversion</a>
<p>
<a href="storage_class.html">Storage classes.</a>
<p>
<a href="cast.html">cast</a>
<p>
<a href="../SYNTAX/typedef.html">typedef</a> keyword.
<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="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
<script language="JavaScript">
<!-- //
document.write(document.lastModified);
// -->
</script>
</address>
|