Difference between typedef and # define ------ [Badboy ],

Source: Internet
Author: User

Difference between typedef and # define ------ [Badboy ],

Typedef and # define are commonly used to define the alias of an identifier and a keyword, but there is a key difference between them.

Typedef is part of the language compilation process;

# Define is a macro Definition Statement, which is not carried out during compilation, but completed in the previous preprocessing process.

To understand the key differences between the two, consider the following:

Think of typedef as a thorough "encapsulation" type, which is equivalent to generating a new variable type (similar to struct, which will be compared with struct for better understanding ). # Define only replaces the macro text.

The difference between typedef and # define is reflected in two aspects:

First, macro type names can be extended with other type specifiers, but the type names defined by typedef cannot be extended.

As follows:

# Define peach int

Unsigned peach I; // No problem. It can be used like this.

Typedef int banana;

Unsigned banana I; // error, invalid!

# Define is only used to replace macro text. It does not generate a new variable type. The preceding code is used as an example. In the pre-processing phase, the peach is replaced with an int, so you can add unsigned before the peach;

Typedef works in a different way than # define. It generates a new variable type, which cannot be extended before it. Similar to the struct type, defining a struct is equivalent to generating a new variable type, and we never see Declarations like unsigned struct student.

The second difference is that in the declaration of several consecutive variables, the type defined by typedef can ensure that all variables in the Declaration are of the same type, and the # define cannot be used.

As follows:

# Define intp int *

Intp a, B;

// After macro expansion, the second line is changed:

Int * a, B;

// A is a pointer variable pointing to the int variable, and B is an int variable! Typedef int * int_p; int_p c, d; // c, d are pointer variables pointing to int variables!

The code above is used as an example. # define is only used for macro replacement. In the preprocessing phase, intp is replaced with int *. This obviously only declares that a is a pointer variable, while B is an int variable. Typedef is equivalent to defining a new variable type, which can work for c and d. Similar to struct, struct student e, f; e and f are declared as student variables.


Typedef and define have different details.

1) # define is a pre-processing command. It is a simple replacement during compilation and pre-processing, and does not check the correctness. It does not mean that it is correct or not, possible errors are detected and reported only when the expanded source program is compiled. For example:
# Define PI 3.1415926
In the program: area = PI * r will be replaced with 3.1415926 * r
If you write the number 9 in the # define statement as a letter g, the preprocessing will also be carried in.

2) typedef is processed during compilation. It gives an existing type an alias in its own scope, but You cannot use the typedef specifier inside a function definition.

3) typedef int * int_ptr;
And
# Define int_ptr int *
Int_ptr is used to represent int *, but the two are different. As mentioned above, # define is replaced simply during preprocessing, while typedef is not a simple replacement, instead, declare a type as if the variable is defined. That is to say;

// Refer to (xzgyb (Lao Damo ))
# Define int_ptr int *
Int_ptr a, B; // equivalent to int * a, B; just a simple macro replacement

Typedef int * int_ptr;
Int_ptr a, B; // a and B all point to the int pointer. If typedef is int *, a new mnemonic is introduced.

This also explains why the following points are true:
// QunKangLi (the maintenance cost is directly proportional to the programmer's creativity)
Typedef int * pint;
# Define PINT int *

So:
Const pint p; // p cannot be changed, but the content pointed to by p can be changed
Const PINT p; // p can be changed, but the content pointed to by p cannot be changed.

Pint is a pointer type. const pint p is used to lock the pointer. p cannot be changed.
Const PINT p is the object indicated by pointer p.

3) You may have noticed that # define is not a statement. Do not add points at the end of the line. Otherwise, a semicolon is replaced.

How to Use typedef

Type, such:

Typedef int;
A B;

// Then B is of the int type.

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.