Application of C language typedef
typedef
Is a keyword in the C language, which is used to define a new name for an existing data type, thereby making the code easier to read and understand. Here are some typedef
application examples:
1. Define the structure
Use typedef
can define a new name for the structure, so that it is more convenient to use in the code.
typedef struct {
int age;
char name[20];
} Person;
Person p;
p.age = 25;
strcpy(p.name, "Tom");
2. Define the pointer type
Use typedef
can define a new name for the pointer type, so that it is more convenient to use in the code.
typedef int* IntPointer;
IntPointer p1, p2;
int a = 10;
p1 = &a;
p2 = p1;
3. Define the function pointer type
Use typedef
can define a new name for the function pointer type, so that it can be used more conveniently in the code.
typedef int (*IntFunc)(int, int);
IntFunc f;
f = add;
int result = f(1, 2);
4. Define the enumerated type
Use typedef
can define a new name for the enumeration type, so that it is more convenient to use in the code.
typedef enum {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
Weekday today = MONDAY;
Use typedef
can make the code more concise and readable, but it should also be used in moderation to avoid excessive use that makes the code difficult to understand.
#define
is a preprocessing directive in C language, which is used to define an identifier as a constant or a macro. Here are some #define
application examples:
1. Define constants
Use #define
can define a constant for more convenient use in code.
#define PI 3.1415926
double area = PI * r * r;
2. Define the macro
Use #define
can define a macro for more convenient use in code.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int max = MAX(3, 5);
3. Define conditional compilation
Using #define
conditional compilation directives can be defined so that different codes can be compiled according to different conditions in the code.
#define DEBUG
#ifdef DEBUG
printf("Debugging!\\n");
#endif
4. Define the type
Use #define
can define a new name for the type, so that it is more convenient to use in the code.
#define MyInt int
MyInt a = 10;
Use #define
can make the code more flexible and easy to use, but it should also be used in moderation to avoid excessive use that makes the code difficult to maintain.
typedef
and #define
are both keywords or instructions in C language used to define new names for existing data types. Their similarities and differences are as follows:
Same point:
- Both can define new names for existing data types.
- Both can make the code more readable and understandable or more flexible and easy to use.
difference:
typedef
It can only be used to define new names for existing data types,#define
not only constants and macros, but also conditional compilation instructions.typedef
The new name defined is a type name, and#define
the new name defined can be a constant, an expression, a statement block, and so on.
Here is a practical example:
The same thing: use typedef
and #define
can define new names for existing data types.
typedef int MyInt;
#define MyInt int
The difference: Use #define
can define constants and macros for more convenient use in code.
#define PI 3.1415926
#define MAX(a, b) ((a) > (b) ? (a) : (b))
The difference: use #define
can define conditional compilation instructions, so that different codes can be compiled according to different conditions in the code.
#define DEBUG
#ifdef DEBUG
printf("Debugging!\\\\n");
#endif
The difference: use typedef
can define a new name for an existing data type, so that it can be used more conveniently in the code.
typedef struct {
int age;
char name[20];
} Person;