In C++, the typedef keyword allows the creation of an alias for existing data types, enabling simpler variable declarations. The document provides syntax examples and advantages of using typedef, such as improved code clarity and portability. It also includes an example of using typedef with enums to define a set of named constants.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
typedef in c++
In C++, the typedef keyword allows the creation of an alias for existing data types, enabling simpler variable declarations. The document provides syntax examples and advantages of using typedef, such as improved code clarity and portability. It also includes an example of using typedef with enums to define a set of named constants.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
C++ typedef
In C++, typedef ( type definition ) is a keyword allows us to create an alias
for existing data types. Once, we have created a type definition, then we can declare a variable using the alias we created. This alias is equivalent to the data type for which it is created. Remember that, in addition to the type definition or alias we are always free to use the original keyword to define variable of any data type. Syntax:-
typedef existing_type new_name;
Example:-
typedef int my_type;
my_type var1, var2, var3;
void main() { typedef int integer;
/* now you can easily use integer to create
variables of type int like this */
integer num1, num2, sum;
cout<<"W3Adda - C++ typedef Example"<<endl; cout<<"Enter two number: "; cin>>num1>>num2; sum=num1+num2; cout<<"Sum = "<<sum;
return ; }
Advantages of typedef
It makes the program more portable.
typedef make complex declaration easier to understand. It can make your code more clear It can make your code easier to modify typedef with enum
typedef enum { sun, mon, tue, wed, thu, fri, sat } weekda y; weekda y day1;