C TypeDef: Defining New Names for Existing Data Types

c typedefC was initially designed to be a system software implementation language. This was back in the 1970s, when computers weren’t all that common and existing languages had several limitations. The C language was far better than its predecessors in many aspects. It was fast, efficient and it could be used for a variety of tasks like creating operating systems and application development. Even today, C continues to be one of the most popular programming languages in the world. It is faster than most other languages, it is supported on almost all systems and the language lets you write portable applications. Most modern day languages, like C++ and Objective C, have inherited several traits from C. Taking C as your first programming language is a very good choice. If you’d like to learn how to program in C, this beginners course is a good place to get started.

In this tutorial, we’re going to take a look a the typedef keyword in C. You need to be familiar with data types in C to understand this tutorial. You can take a look at some of the tutorials we’ve written on data types to get acquainted (or reacquainted) with them.

The typdef Keyword

The typedef keyword lets you give a new name to an existing data type or structure. For example, if you had a integer data type and you wanted to refer to it by some other name in your program, you can with the typedef keyword. The general syntax for the typedef keyword is as follows:

typedef current_name new_name;

If you had an integer data type, you can change its name. Take a look at this example:

typedef unsigned int age;

Here, the unsigned int data type can now be called ‘age’ in your program. Instead of declaring variables belonging to the unsigned int data type, you can declare variables belonging to the age data type. The new variables will hold the same format of mathematical values as other unsigned int data types – the only difference will be that they will reference age instead of unsigned int:

age client1, client2;

Learn more about the different data types in C with this course.

The Use of typedef

So why do we need the typedef keyword exactly? We should be fine with just a normal data type declaration for all our variables, right? The typedef keyword is mostly used in large, complex programs or applications where it’s important to keep all the data straight – especially when the data is stored in structures, and often nested structures. The typedef keyword helps improve readability and avoid mistakes.

Suppose you are creating an application that is keeping track of all the people attending a gym and the progress they’re making. It will want to record peoples’ height, their weight, weight gain or loss, the number of months they’ve attended the gym and their current age, among other numerical values. In this case, we know that all values we are going to track are numerical- that is, they will either be an integer data type or a float (decimal) data type. You could, of course, declare all variables in your application as float. That would have worked if your application didn’t have many variables. But because your app is tracking so many variables (and people), it is often difficult to differentiate between them. You might end up making a mistake and store a value in a wrong variable. You don’t want to confuse weight lost with weight gained, for example, and have to face irate customers. In these cases, to improve readability and to avoid programming mistakes, you use the typedef keyword. You can just declare weight_lost and weight_gained as two separate data types. This also makes it easier to spot problems in your program later, and you will have an easier time with making upgrades too.

Another way to avoid mistakes like that is to use wrapper classes. Unfortunately, wrapper classes aren’t supported in C (they are a C++ resource). In C, the typedef works just as well. You can learn more about good programming practices in C and avoiding mistakes by taking this C course.

Examples of typedef

The typedef keyword can be used to define structures. For example, take a look at the program below:

#include< stdio.h>
#include< string.h>
#include< conio.h>
typedef struct gym_clients
{
char  name[50];
int   membershipcost;
} gym;
void main( )
{
gym client1;
printf("\nAdd Client Info:\n");
printf("\nEmployee name\t");
scanf("%s",client1.name);
printf("\nMembership Plan Cost \t");
scanf("%d",&client1.membershipcost);
printf("\nsClient name is %s",client1.name);
printf("\nClient has paid %d dollars",client1.membershipcost);
getch();
}

Output:

Add Client Info
Employee name
Membership Plan Cost
Client name is John
Client has paid 400 dollars

This is a simple program that accepts client information and prints it out. The important part, to us, is the typedef declaration we used on the structure. The structure “gym_clients” will be referred to as “gym” in our program because of the typedef declaration. The rest of the program deals with adding client information, storing it in the structure and finally displaying the information on the screen.

You can use typedef to declare multiple pointers in a single line. For example, take a look at the following code:

int *a, b;

Here, the variable x is declared as pointer of an integer type. However, y does not become an integer type pointer. You would have to declare it separately:

int *a;
int *b;

If you had several pointers to declare, it could be annoying to declare them all separately. In such cases, you can use the typedef declaration:

typedef int* Pointer;
Pointer a, b, c, d, e, f;

This both saves you effort and makes your code more readable.

The typedef keyword is all about making your program easier to read and helping you avoid mistakes. It also makes it easier to change your program later. To learn more interesting concepts in C, sign up for this C course.