Typedef and # define definitions and differences

Source: Internet
Author: User

I. Usage of typedef

In C/C ++, typedef is often used to define an alias for an identifier and a keyword. It is part of the language compilation process, but it does not actually allocate memory space, such:

Typedef int;
Typedef int array [10];
Typedef (int *) pint;

Typedef can be enhancedProgramBut it also has some disadvantages such as "non-intuitive.

Ii. # define usage

# Define is a macro definition statement. It is usually used to define constants (including non-parameters and parameters), and to implement macros that are "superficial and kind, long strings behind them, it is not carried out in the compilation process, but completed before this (preprocessing process), but it is difficult to find potential errors and otherCodeMaintenance problem. Its instance is like:

# Define int
# Define true 1
# Define add (A, B) (a) + (B ));
# Define loop_10 for (INT I = 0; I <10; I ++)

In clause 1 of Scott Meyer's Objective C ++, we have analyzed the disadvantages of # define statements and good alternatives. For more information, see.

Iii. Differences between typedef and # define

From the above concepts, we can also understand that typedef is just a new name (only an alias) for the identifier to increase readability, and # define is originally used to define constants in C, the emergence of C ++, const, Enum, and inline has gradually become an alias tool. Sometimes it is easy to figure out which one should be used for typedef and which one should be used, for example, a statement such as # define int, which one can be used for the same purpose? I advocate the use of typedef, because in many early C compilers, this statement was invalid, but today's compiler has expanded. To be as compatible as possible, # define is generally followed to define "readable" constants and macro statement tasks, while typedef is often used to define keywords and lengthy type names.

Macro definition is just a simple string replacement (in-situ extension), while typedef is not in-situ extension, and its new name has a certain encapsulation, so that the new name identifier has a more convenient function to define variables. See the third line of the first code:

Typedef (int *) pint;
And the following line:
# Define pint2 int *

Same effect? Actually different! In practice, see the difference: pint a, B; has the same effect as int * A; int * B; indicates that two integer pointer variables are defined. Pint2 a, B; has the same effect as int * a, B; indicates defining an integer pointer variable A and an integer variable B.

Note: There is another line at the end of the two; the difference between the two!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.