typedef in C/C++ [Quiz Included]
In this tutorial, we will discuss:
- typedef in C/C++
- Syntax of typedef in C/C++
- Examples of typedef in C/C++
- Difference between #define and typedef
After completing this tutorial, all your concepts involving #define and typedef would be crystal clear.
What is typedef in C and C++?
As the name itself suggests, typedef stands for “type definition”. typedef is nothing but a way to assign a new name to a pre-existing data type. In other words, typedef is basically a reserved keyword that we use in order to create an alias name for a specific data type.
The function performed or purpose served by the data type remains the same. We simply use it to reduce the complexity of the code for the programmer’s convenience, while declaring higher-level data types like structures and unions.
Get a Complete Overview of Data Types in C Programming Language
Syntax of typedef
The syntax of C typedef is as simple as its use.
typedef data_type new_data_type_name;
For instance,
typedef signed int positive_integer;
After assigning the alternative name for signed int, we have to use positive _integer as a data type while naming identifiers for our program.
How can we use typedef in C?
Here is a code in C which illustrates the use of typedef:
#include<stdio.h> int main() { printf("Welcome to DataFlair tutorials!\n\n"); typedef signed positive_int; // Use of typedef keyword positive_int number = 80; printf("The number is: %d\n",number); return 0; }
Code on Screen-
Output-
How can we use typedef in C++?
Here is a code in C++ which illustrates the use of typedef:
#include <iostream> using namespace std; int main() { cout<<"Welcome to DataFlair tutorials!"<<endl<<endl; typedef signed positive_int; // Use of typedef keyword positive_int number = 80; cout<<"The number is: "<< number <<endl; return 0; }
Code-
Output-
Example of typedef in C
Now that it is pretty clear as to what typedef is, let us consider a simple problem at hand to acknowledge its significance.
Suppose, we are expected to create a structure named student with details such as name and age, instead of writing the keyword struct followed by its structure name, we could use an alternative name in order to maintain the clarity of the code. A good programmer knows what to use and how to increase the readability of the code.
Here is a code in C which illustrates the use of typedef with reference to the above-stated problem:
#include<stdio.h> typedef struct student { char name[30]; int age; }stud; // Note that stud is not an object, it is an alternative name for structure data type int main( ) { stud s; // s is an object of the structure printf("Welcome to DataFlair tutorials!\n\n"); printf("Enter student name: "); scanf("%s", s.name); printf("Enter student age: "); scanf("%d", &s.age); printf("Student name is: %s\n", s.name); printf("Student age is: %d\n", s.age); return 0; }
Code on Screen-
Output-
Example of typedef in C++
Here is a code in C++ which illustrates the use of typedef with reference to the above-stated problem:
#include<iostream> using namespace std; typedef struct student { string name; int age; }stud; // Note that stud is not an object, it is an alternative name for structure data type int main( ) { stud s; // s is an object of the structure cout<<"Welcome to DataFlair tutorials!"<<endl<<endl; cout<<"Enter student name: "<<endl; cin>>s.name; cout<<"Enter student age: "<<endl; cin>>s.age; cout<<"Student name is: "<< s.name <<endl; cout<<"Student age is: "<< s.age <<endl; return 0; }
Code-
Output-
Difference Between typedef and #define
If you don’t even have the slightest bit of idea as to what #define is, refer to Macros. We already know that #define is a C preprocessor directive. Since both typedef and #define are used to provide alternative names to existing types, what is the difference between the two?
Here is a table that illustrates the difference between typedef and #define
typedef | #define |
typedef can provide alternative names to data types only. It cannot do the same with values. | It can provide alternative names to values as well. For example, 3.14159 can be defined as pi. |
typedef is a compiler-based. | It is preprocessor-based. |
It has to be terminated with the help of a semicolon. (;) | define cannot be terminated with the help of a semicolon. (;) |
It describes the actual definition of a new type. | When used, it simply copies and pastes the definition values. |
If a new type is defined inside a function, it will be visible until the scope is present. | It does not follow scope rule. |
Quiz on typedef in C
Summary
In this tutorial, we discussed the basic meaning behind the typedef in detail and inferred that it is nothing but an alternative name assigned to an existing data type. Then, we discussed its basic syntax and significance that helped us infer how easy and convenient it is to use a typedef. Finally, we concluded our discussion by understanding the key difference between typedef and #define.
Now, it’s time to learn Functions in C language
Comments are the best way to express your experience.
Your opinion matters
Please write your valuable feedback about DataFlair on Google
Very helpful tutorial. Here is my own code for fun 🙂
/* typedef: a keyword used to assign alternative names (alias) to the existing data types –mostly used with user-defined data types such as structure and union
*/
#include
#include
using namespace std;
typedef struct student
{
string name;
int roll;
int age;
}stud; // Note that stud is not an object, it is an alternative name for structure data type
int main()
{
stud st ={“Tibebu Sime”,11592713, 38}; //st is an object of the structure
cout<<"Student name is: "<<st.name<<endl;
cout<<"Student enrollment number is: "<<st.roll<<endl;
cout<<"Student age is: "<<st.age<<endl;
return 0;
}