| by Arround The Web | No comments

Typedef in C

The typedef is the predefined keyword, which instructs the compiler to assign the user-defined types to the predefined types of C, i.e., int, float, decimal, double float, etc.

Typedef can be used when it is difficult to type multiple times in a program. For example, multiple-time use of “unsigned int” increases the complexity of the program. To reduce that complexity, we can define a user-friendly name for that type. For example, if we are using it in student ID, marks, etc., then we can simply name it “student”.

In simple words, we can say that typedef reduces the complexity of the program and also provides clarity of the code.

Uses of typedef

There are two types of the uses of typedef keyword.

The first one is with the predefined data type. When it comes to some complex data types and we have to create more than one variable, we will use typedef to reduce the complexity of the code.

Syntax

In the snippet above, we can see the way to use typedef using a simple predefined data type in which we first write the keyword “typedef” and then the datatype that is to be aliased, i.e., unsigned int etc., and then the name that is to be used for that type.

Example 1

In this piece of code, we will be using the typedef keyword to create an alias for our predefined data type. After including our header file “stdio.h”, which allows us to perform input-output operations, we move to our main() function where we define variables using the typedef keyword. In this program, we are using a long long int data type. We first write the keyword typedef and then the datatype that is long long int. As we can see, long long int is feasible to use once, but if we have to use it more than once, it will increase the size of the code. Then we used an alias “lng” for our datatype.

#include <stdio.h>
#include <limits.h>
int main()
{
   typedef long long int lng;
   printf("The memory byte long long int takes are : %ld \n", sizeof(lng));
   return 0;
}

Then we used the printf() statement to print the size of the datatype that we are going to use. In our printf() statement, we pass the method sizeof() that is responsible for checking the size of the variable that is passed to it. In our case, we are checking for the size of the datatype that is “8”.

As shown in the screenshot below, our datatype size is successfully printed.

Example 2

As we have studied above, typedef reduces the complexity of code. Typedef can also be used with pointers. We know that pointers are the most important in the C language. Let’s begin with our code. First, we will include our header files, and then, outside the main function, we will declare a typedef of an integer type pointer that is “integer_ptr”. Now in our main function, we will create an integer pointer “a” that will point to the address of the integer type variable we have declared in our next line, which is “a_address” and assign it the value “125023”.

#include<stdio.h>
typedef int* integer_ptr;

int main( )
{
    integer_ptr a;
    int a_addres = 125023;
    a = &a_addres;    
    printf("%p",a);                
   
}

In the next line, we have assigned the pointer “a” the address of the variable a_address using the keyword ampersand “&.” The ampersand is used to reference the address of the variable that is stored in memory. In this case, we are referencing the address of variable “a_address”.

At last, we will print the memory address of the variable “a_address” using the printf() statement in which we passed “%p.” This denotes the pointer type data and “a” that holds the address. The variable “a_address” is “0x7ffe889bd77c”.

As we know, the structure is also a user-defined data type. Sometimes it is complex and difficult to create objects from them, so we use typedef to reduce the size of the structure.

Syntax

The structure is used to combine multiple data types into a single type that is a user-defined type. Sometimes it is difficult to use multiple times structure datatype because of its complexity, so to reduce this factor, we use the typedef keyword to define a user-friendly alias. We first write the keyword typedef and then the struct keyword that is used to create a structure. After that, the name is given to that type.

Example 3

In this example, we are going to manipulate students’ records, and we will learn how to use the typedef keyword with the structure function. As we are going to work with strings and characters, we will include both header files “stdio.h” and “string.h”. After the header files, we will define or type. To do so, we will first write the keyword typedef and then the struct keyword because we are going to group multiple datatypes in one type, then name it “Student”. Inside the braces, we will initialize all variables and arrays which will be accessed throughout the program, so we don’t have to define them again and again.

#include <stdio.h>
#include <string.h>
typedef struct Student{
   char name[50];
   char course[50];
   int std_id;
} Student;
int main( ) {
   Student stud;
   strcpy( stud.name, "Mudasir");
   strcpy( stud.course, "BS SE");
   stud.std_id = 6495407;
   printf( "Student name is : %s\n", stud.name);
   printf( "Student course is : %s\n", stud.course);
   printf( "Student roll no is : %d\n", stud.std_id);
   return 0;
}

Now moving towards our main() function where we define an object of our structure type that is “stud”. The “stud” now holds all of the datatypes that we have declared outside of the main function. One thing to keep in mind is that whenever we call the structure datatype, we have to add a dot “.” between the object and the variable we are going to call from the structure class. These variables are always called using the syntax:

$ object_defined.variable_name

Firstly, we will pass the name of the student using the “stud” object and the name that is “mudasir” to the strcpy() function. The strcpy() function is responsible for copying the passed data to the variable. In this, we are copying the name “mudasir” to the structure variable “stud”.we will repeat the same step for the course. And after that, simply assign the id to the “std_id” variable that is “6495407”.

Now we are printing our student data using the printf() statement in which we passed the “stud.name” which is responsible for holding the name of the student, %s that is responsible for displaying the string, repeating the same step in the next line for displaying course and as well for the ID of the student. In the case of ID, it is an integer type, so we passed the %d in this statement, which means it will display the decimal type of data.

As shown in the given output student, name, student course, and the ID of the student are displayed.

Conclusion

In this guide, we have learned about the typedef keyword and its multiple uses. We have also used it in multiple ways, using pointers, structures, and simple predefined data types.

Share Button

Source: linuxhint.com

Leave a Reply