The typedef is a keyword that is used to provide existing data types with a new name. The C typedef keyword is used to redefine the name of already existing data types.
When names of datatypes become difficult to use in programs, typedef is used with user-defined datatypes, which behave similarly to defining an alias for commands.
C typedef Syntax
typedef existing_name alias_name;
After this declaration, we can use the alias_name as if it were the real existing_name in out C program.
Example of typedef in C
typedef long long ll;
Below is the C program to illustrate how to use typedef.
C
#include <stdio.h>
typedef long long ll;
int main()
{
ll var = 20;
printf ( "%ld" , var);
return 0;
}
|
Use of typedef in C
Following are some common uses of the typedef in C programming:
- The typedef keyword gives a meaningful name to the existing data type which helps other users to understand the program more easily.
- It can be used with structures to increase code readability and we don’t have to type struct repeatedly.
- The typedef keyword can also be used with pointers to declare multiple pointers in a single statement.
- It can be used with arrays to declare any number of variables.
1. typedef struct
typedef can also be used with structures in the C programming language. A new data type can be created and used to define the structure variable.
Example 1: Using typedef to define a name for a structure
C
#include <stdio.h>
#include <string.h>
typedef struct students {
char name[50];
char branch[50];
int ID_no;
} stu;
int main()
{
stu st;
strcpy (st.name, "Kamlesh Joshi" );
strcpy (st.branch, "Computer Science And Engineering" );
st.ID_no = 108;
printf ( "Name: %s\n" , st.name);
printf ( "Branch: %s\n" , st.branch);
printf ( "ID_no: %d\n" , st.ID_no);
return 0;
}
|
Output
Name: Kamlesh Joshi
Branch: Computer Science And Engineering
ID_no: 108
2. typedef with Pointers
typedef can also be used with pointers as it gives an alias name to the pointers. Typedef is very efficient while declaring multiple pointers in a single statement because pointers bind to the right on the simple declaration.
Example:
typedef int* Int_ptr;
Int_ptr var, var1, var2;
In the above statement var, var1, and var2 are declared as pointers of type int which helps us to declare multiple numbers of pointers in a single statement.
Example 2: Using typedef to define a name for pointer type.
C
#include <stdio.h>
typedef int * ptr;
int main()
{
ptr var;
*var = 20;
printf ( "Value of var is %d" , *var);
return 0;
}
|
Output
Value of var is 20
3. typedef with Array
typedef can also be used with an array to increase their count.
Example:
typedef int arr[20]
Here, arr is an alias for an array of 20 integer elements.
// it's same as Arr[20], two-Arr[20][23];
arr Arr, two-Arr[23];
Example 3: Using typedef to define an alias for Array.
C
#include <stdio.h>
typedef int Arr[4];
int main()
{
Arr temp = { 10, 20, 30, 40 };
printf ( "typedef using an array\n" );
for ( int i = 0; i < 4; i++) {
printf ( "%d " , temp[i]);
}
return 0;
}
|
Output
typedef using an array
10 20 30 40
C typedef vs #define
The following are the major difference between the typedef and #define in C:
- #define is capable of defining aliases for values as well, for instance, you can define 1 as ONE, 3.14 as PI, etc. Typedef is limited to giving symbolic names to types only.
- Preprocessors interpret #define statements, while the compiler interprets typedef statements.
- There should be no semicolon at the end of #define, but a semicolon at the end of typedef.
- In contrast with #define, typedef will actually define a new type by copying and pasting the definition values.
Below is the C program to implement #define:
C
#include <stdio.h>
#define LIMIT 3
int main()
{
for ( int i = 0; i < LIMIT; i++) {
printf ( "%d \n" , i);
}
return 0;
}
|
FAQs on typedef in C
1. What is typedef in C?
The C typedef statement defines an alias or a nickname for the already existing data type.
2. What is typedef struct?
The typedef struct is the statement used to define an alias for the structure data type.
3. What is typedef enum?
The typedef enum is used to define the alias for the enumeration data type.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Apr, 2023
Like Article
Save Article