The typedef operator lets you rename a data type, like creating an alias. One of its main uses is to increase the portability of C applications: a common header file can include aliases to specific data types for different computers. By changing the particulars of the header file, the application should work on another system.
As an example, before the C99 standard supported the boolean type, you could have used typedef to create it yourself (which would store 0 or 1):
typedef int boolean; boolean correct;
The typedef operator can be used nicely with structures to simplify your declarations:
structure students { char name[30]; gpa float; };
The formal way to create a variable of this structure's type is
struct students timmy;
By using typedef, you can create an alias:
typedef struct students student_type;
Now, to declare a variable of this structure's type, just use
student_type timmy;
The typedef line of code makes the word student_type act as shorthand for struct students.
Using typedef like this can make your code easier to type and easier to read. It will be used in our next example, read_weather, which will retrieve all the data from the binary weather.dat file and display it.
1. | Create a new file or project in your text editor or IDE. |
2. | Type the standard beginning lines of code (Script 13.2). /* read_weather.c - Script 13.2 */ #include <stdio.h> Script 13.2. The typedef operator can help shorten how you refer to structures. This application uses those structures to read data from a binary file. |
3. | Set the file path and name as a C preprocessor macro: #define THEFILE "/Users/larry/Desktop/weather.dat" or #define THEFILE "C:\Documents and Settings\Larry Ullman\Desktop\ weather.dat" This should point to the exact same file we used in weather_structure.c (Script 13.1). |
4. | Begin the main function: int main (void) { |
5. | Define the structure: struct weather_record { char date[11]; int high; int low; }; The structure also matches the same structure defined in the previous example. |
6. | |
7. | Create a file pointer and open the file for binary reading: FILE *fp; fp = fopen(THEFILE, "rb"); if (fp != NULL) { |
8. | Read in the contents of the data file in a loop: while (fread (&day, sizeof(wr), 1, fp)) { This line says that 1 block of sizeof(wr) bytes should be read from the file and assigned to day. Without using the typedef operator, the second parameter would have to be formally written out as sizeof(struct weather_record). As long as this condition is true—that a block could be read from the binary file—the contents of the loop will be executed. |
9. | Print each record: printf ("Date: %s High: %d Low: %d ", day.date, day.high, day.low); A simple print statement is used to send each record to the standard output. To access the individual members (which are stored in the day structure), use the day.membername syntax. |
10. | Complete the while loop: } |
11. | Complete the fp conditional: } else { printf ("The file could not be opened. "); return 1; } |
12. | |
13. | Complete the main function: getchar(); return 0; } |
14. | Save the file as read_weather.c, compile, and debug as necessary. |
15. | Run the application (Figure 13.6). Figure 13.6. This application uses structures to parse individual elements out of a binary file. |
✓ Tips
Although you should place your typedef statements toward the top of your code, the only requirement is that they are defined before they are used.
When you have a series of applications all of which use the same data file and structure definition (like weather_structure and read_weather), you could benefit from using an external library file. Place the #define macro and the structure definition is a plain-text file called defs.h and then include this in your other C files.
Overusing typedef or using it in ways that's not clear can lead to confusing code. Each alias you make should be fairly obvious and useful.