What is the typedef enumeration in C.

15.11 Type definition with ┬╗typedef┬ź

With the keyword typedef a new identifier can be used for a simple data type. The syntax of a simple type definition looks like this:

typedef type definition identifier;

This can significantly improve the readability of a program. This type definition with typedef is to be demonstrated using the address program. Here you can see the listing:

/ * typedef1.c * / #include #include #include #define MAX 30 static int x; struct address {char vname [MAX]; char nname [MAX]; long ZIP; char location [MAX]; int year of birth; } addresses [100]; typedef struct adres ADDRESS; void input (int no, ADDRESS * new) {printf ("first name:"); fgets (new [no] .vname, MAX, stdin); printf ("Last Name:"); fgets (new [no] .nname, MAX, stdin); printf ("Postcode:"); do {scanf ("% 5ld", & new [no]. ZIP); } while (getchar ()! = '\ n'); printf ("City:"); fgets (new [no] .ort, MAX, stdin); printf ("year of birth:"); do {scanf ("% 4d", & new [no]. year of birth); } while (getchar ()! = '\ n'); } void search (ADDRESS * search, char letter, int no) {int i; for (i = 0; i <= nr; i ++) {if (search [i] .nname [0] == letter) {printf ("\ n \ nFound under letter: \"% c \ "\ n \ n ", Letter); printf ("First name .......:% s", search [i] .vname); printf ("Last Name ......:% s", search [i] .nname); printf ("Postcode ..:% ld \ n", search [i] .PLZ); printf ("Location ...........:% s", search [i] .ort); printf ("year of birth ...:% d \ n", search [i]. year of birth); printf ("\ n \ tContinue with \ n"); getchar (); }}} void output (ADDRESS * all, int no) {int i; for (i = 0; i \ n \ n"); getchar (); }}} void Sort (ADDRESS * sort, int no) {ADDRESS * temp; int i, j; temp = malloc (sizeof (ADDRESS *)); if (NULL == temp) {printf ("Couldn't reserve memory ... \ n"); return; } for (i = 0; i 0) {* temp = sort [j]; sort [j] = sort [i]; sort [i] = * temp; }}} printf ("... sorted !! \ n"); } int main (void) {int selection; char c; do {printf ("- 1- Enter new address \ n"); printf ("- 2- Print specific address \ n"); printf ("- 3- Print all addresses \ n"); printf ("- sort 4 addresses \ n"); printf ("- 5- quit program \ n"); printf ("\ nYour choice:"); scanf ("% d", & selection); / * fflush (stdin); * / getchar (); switch (selection) {case 1: input (x ++, addresses); break; case 2: printf ("First letter surname:"); do {scanf ("% c", & c); } while (getchar ()! = '\ n'); Search (addresses, c, x); break; case 3: output (addresses, x); break; case 4: Sort (addresses, x); break; default: break; }} while (selection <5); return EXIT_SUCCESS; }

Thanks to the new type definition

typedef struct adres ADDRESS;

can use the structure now

ADDRESS newaddresses [100];

can be accessed. This is much easier to read with longer programs. This is particularly useful when there are several structures that are very similar in structure to one another. The type definition in the program could also be defined differently:

typedef struct adres {char vname [20]; char nname [20]; long ZIP; char location [20]; int year of birth; } ADDRESS; ... ADDRESS addresses [100];

The type definition can also be applied to other variables. The following definitions can be seen quite often:

typedef unsigned char BYTE; // 1 byte = 8 BIT typedef unsigned int WORD; // 1 WORD = 16 BIT typedef unsigned long DWORD; // 1 DOUBLE WORD = 32 BIT typedef unsigned double QWORD; // 1 QUAD WORD = 64 BIT typedef unsigned int uint; typedef unsigned char uchar;

For example, after that is the following notation

equivalent to:

unsigned int value1, value2;

The typedef keyword is also used to create so-called primitive data types. What's that good for? Let's take the primitive data type uclock_t as an example (primitive data types usually always end with _t). This is defined in the header file with:

On another system, this definition might look like this:

typedef unsigned int uclock_t;

The primitive data types make a program more portable. This means that you do not have to deal with the data types when porting to other systems. For example, if you programmed a program on a 32-bit system and then tested it on a 16-bit system, finding the wrong value error can be frustrating.

As with enum or define, the program does not get better or faster with typedef, but it only serves to make your program easier to read, write and port to other systems.

your opinion

How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]