diff options
Diffstat (limited to 'reference/C/SYNTAX/typedef.html')
-rw-r--r-- | reference/C/SYNTAX/typedef.html | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/reference/C/SYNTAX/typedef.html b/reference/C/SYNTAX/typedef.html new file mode 100644 index 0000000..e9cb665 --- /dev/null +++ b/reference/C/SYNTAX/typedef.html @@ -0,0 +1,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> + |