Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The difference and relation between typedef and define

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

In C or C++, you can replace existing types by using typedef and define to declare new types. For example:

Typedef int COUNT

# define COUNT int

The function of both is to represent int with COUNT. Although the functions of the two are the same, they are actually very different, otherwise they would not exist at the same time. Let's briefly talk about the difference between the two, hoping to make the right choice when using it, so as to reduce unnecessary trouble:

Typedef

In C or C++, typedef is often used to define an alias for identifiers and keywords. It is part of the language compilation process, but it does not actually allocate memory space.

Usage: (1) first write the definition body according to the method of defining variables (such as int n;)

(2) change the variable name to a new type name (for example, n-> NUM)

(3) add typedef at the beginning (e.g. typedef NUM [100])

(4) variables can be defined with new type names.

It can also be used in C to declare the structure type:

Typedef struct

{double score

String name

Int data

} SCORE

This completes the declaration of the new type name SCORE, which represents the structure type specified above, and then you can use SCORE to define the variable: SCORE FIRST

SCORE * p; / / p is a pointer to this structure type data

Note: (1) typedef can declare various type names, but it cannot be used to define variables.

(2) typedef simply assigns a new type name to an existing type, but does not create a new type.

(3) typedef can facilitate the generalization and transplant of the program. For example, different hardware platforms have different definitions of int length, some are 2 bytes and some are 4 bytes. If you migrate a program from a 4-byte computer system that stores integer data to a system that stores × × data with 2 bytes, you need to replace all int types in the definition variables with long types, but if you use typedef to define int types, you only need to change one place.

2.#define

Defining statements for macros, which are usually used to define constants, does not take place during compilation, but is completed before that, but makes it difficult to find potential errors and other code maintenance problems.

The difference between the two:

# define is handled during precompilation, and it only makes simple replacements. Typedef, on the other hand, is handled at compile time and is not just a simple replacement. For example:

Typedef int NUM [10]

Instead of using NUM [10] to replace "int", 20 declares a type in the same way as defining a variable, and then uses it to define a variable.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report