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
|
<html>
<head>
<title>The TYPEDEF keyword.</title>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center><h1>The TYPEDEF keyword.</h1></center>
<hr>
<p>
Every variable has a
<a href="../CONCEPT/data_types.html">data type</a>. <b>typedef</b> is used to define
new data type names to make a program more readable to the programmer.
<p>
For example:
<p>
<table border=1 width="100%" bgcolor="ivory">
<tr><td>
<pre>
|
main() | main()
{ | {
int money; | typedef int Pounds;
money = 2; | Pounds money = 2
} | }
</pre>
</td></tr></table>
<p>
These examples are EXACTLY the same to the compiler. But the right hand
example tells the programmer the type of money he is dealing with.
<p>
<a name=boolean></a>
A common use for typedef is to define a boolean data type as below.
<font color="brown">
<br>
Note: Recent C++ compilers have introduced a <a href="../../CPLUSPLUS/SYNTAX/bool.html">boolean</a> datatype.
<p>
</font>
<p>
<table border=1 width="100%" bgcolor="ivory">
<tr><td>
<pre>
typedef enum {FALSE=0, TRUE} Boolean
main ()
{
Boolean flag = TRUE;
}
</pre>
</td></tr></table>
<p>
And as a final example, how about creating a string datatype?
<p>
<table border=1 width="100%" bgcolor="ivory">
<tr><td>
<pre>
typedef char *String;
main()
{
String Text = "Thunderbird";
printf("%s\n", Text);
}
</pre>
</td></tr></table>
<p>
The main use for typedef seems to be defining
<a href="struct.html">structures</a>. For example:
<p>
<table border=1 width="100%" bgcolor="ivory">
<tr><td>
<pre>
typedef struct {int age; char *name} person;
person people;
</pre>
</td></tr></table>
<p>
Take care to note that <b>person</b> is now a type specifier and NOT a
variable name.
<p>
As a final note, you can create several data types in one hit.
<table border=1 width="100%" bgcolor=ivory>
<tr><td>
<pre>
typedef int Pounds, Shillings, Pennies, Dollars, Cents;
</pre>
</td></tr></table>
<p>
<p>
<hr>
<h2>Examples:</h2>
<img src="../../GRAPHICS/computer.gif" align=center>
<a href="../EXAMPLES/linklst3.c">Here is a rather heavy example of typedef.</a>
<br clear=left>
<hr>
<h2>Notes:</h2>
I would expect to see 'typedef' in header files.
<p>
<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="keywords.html"> Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|