The similarities and differences between C language typedef and #define

Application of C language typedef

typedefIs 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 typedefapplication examples:

1. Define the structure

Use typedefcan 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 typedefcan 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 typedefcan 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 typedefcan 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 typedefcan 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.

#defineis a preprocessing directive in C language, which is used to define an identifier as a constant or a macro. Here are some #defineapplication examples:

1. Define constants

Use #definecan define a constant for more convenient use in code.

#define PI 3.1415926

double area = PI * r * r;

2. Define the macro

Use #definecan 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 #defineconditional 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 #definecan 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 #definecan 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.

typedefand #defineare 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:

  • typedefIt can only be used to define new names for existing data types, #definenot only constants and macros, but also conditional compilation instructions.
  • typedefThe new name defined is a type name, and #definethe new name defined can be a constant, an expression, a statement block, and so on.

Here is a practical example:

The same thing: use typedefand #definecan define new names for existing data types.

typedef int MyInt;
#define MyInt int

The difference: Use #definecan 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 #definecan 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 typedefcan 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;

Guess you like

Origin blog.csdn.net/weixin_51624736/article/details/129500268