Examples of Using the “typedef” Keyword in C in Linux
Generally, there are two most common uses of the “typedef” keyword in the C programming language. The first usage is with the pre-defined data types. There are certain complex pre-defined data types such as “unsigned int”. If you are going to create multiple variables of this data type within your code, then it will get very inconvenient for you to type the name of this data type every time you attempt to create such a variable in your code. To avoid this difficulty, you can create a short name or an alias of this data type with the help of the “typedef” keyword, and then you can use that alias throughout your code for variable declaration of this type.
The second usage is with the structures in the C programming language. A structure itself is a user-defined data type. However, at times, the name of your structure is so complex that it creates trouble for you whenever you attempt to create an object of that structure. To resolve this issue, you can shorten the actual name of your structure by devising its alias with the help of the “typedef” keyword, and then you can use that alias to create the objects of the said structure throughout your code.
Now, we will go through a few examples that will make the concept of using the “typedef” keyword in C even clearer.
Example 1: Using the “typedef” Keyword with Normal Data Types in C
In this example, we will be using the “typedef” keyword with the normal data types in C. For that, we have written a sample C program, which is shown in the following image. In this code, we wanted to declare two variables of the “unsigned int” data type. However, we do not need to use this long declaration every time. Therefore, we have first defined an alias for this data type with the “typedef” keyword. We named this alias “uint”. This definition has the following three parts: the “typedef” keyword, the data type whose alias you want to create, and the alias itself. After this definition, we have simply used the alias “uint” for declaring two different variables “var1” and “var2” having the “unsigned int” data type. Then, we have simply printed the values of these variables on the terminal. From this code, you can easily see how we shortened the name of a regular data type by using the “typedef” keyword.
After saving this code, you can compile it with the command cited below:
When this code is compiled successfully, we can run it using the following command:
The output of this code shows that there were no errors in our code, which means that we have successfully used the alias of the “unsigned int” data type that has served the intended purpose by making use of the “typedef” keyword.
Example 2: Using the “typedef” Keyword with Structure in C
Apart from simply using the “typedef” keyword with the normal data types, it can also be used to shorten the regular names of the structures in the C programming language. In the C code that is shown in the image below, we have defined a structure named “Student”. However, instead of using the regular structure definition in C, we have also stated the “typedef” keyword before this definition. This is done to create an alias for the “Student” structure. The alias that we have created for this structure is “Stud”, which is written after the closing brackets of our structure, followed by a semi-colon. In the body of this structure, we have some attributes such as “name”, “degree”, “university”, and “id”.
After creating this structure, we have defined the “main()” function of our program. In the body of this function, we have first created an object of our structure. You can easily see from our code that we have not used the regular name of our structure, i.e., Student for creating its object rather we have used its alias, i.e., Stud to create the object. After creating the object “S”, we have used the “strcpy()” function of the C programming language (which is used to copy one string to another) to copy the desired values to the string variables within our structure. However, the “id” can be assigned an integer value through the regular assignment method. Finally, we have printed all these values on the terminal.
This code can be easily compiled and executed in the same manner as we discussed in example #1. You can visualize its output in the appended image:
This article provided a good head start to the beginners for using the “typedef” keyword in the C programming language in Linux. After learning the basic usage of this keyword, you can even create more complex programs with it.