|
typedef Any-data-type Single-word-alias ;
|
Example:
typedef int my_int ; |
This typedef clause will define my_int as an alias for int
typedef int my_int;
int main(int argc, char *argv[])
{
int a;
my_int b; // b is actually an int !!!
a = 123;
printf("a = %d\n", a);
/* ==================================================
If my_int is really a NEW type, then you CANNOT
assign an int to an my_int variable !!!
=================================================== */
b = a; // Proof that my_int is no different than int
printf("b = %d\n", b);
}
|
How to run the program:
|
|
/* ----------------------------------------------- The struct definition and the typedef should be placed inside a header file and included by the main program file.... ----------------------------------------------- */ struct BankAccount { int accNum; double balance; }; typedef struct BankAccount BankAccount_t ; // new name (alias) int main(int argc, char *argv[]) { BankAccount_t b; // We can use the typedef-ed name struct BankAccount a; // AND you can STILL use the original method a.accNum = 123; a.balance = 4000; printf("a.accNum = %d a.balance = %f\n", a.accNum, a.balance); /* =============================================== Proof that they are the same type of variable ================================================ */ b = a; // You can assign them to each other ! printf("b.accNum = %d b.balance = %f\n", b.accNum, b.balance); } |
Result:
a.accNum = 123 a.balance = 4000.000000 b.accNum = 123 b.balance = 4000.000000 |
How to run the program:
|
typedef struct/union definition new-name ; |
Example:
typedef struct BankAccount { int accNum; double balance; }BankAccount_t; int main(int argc, char *argv[]) { struct BankAccount a; // You STILL can use struct BankAccount ! BankAccount_t b; a.accNum = 123; a.balance = 4000; printf("a.accNum = %d a.balance = %f\n", a.accNum, a.balance); b = a; printf("b.accNum = %d b.balance = %f\n", b.accNum, b.balance); } |
How to run the program:
|
|