# What is the difference between define and typedef?

Source: Internet
Author: User

Typedef int * pint; // occurs during compilation

It is equivalent to an int * with an alias. It can be used to define an integer pointer and is also a type name.

Pint p, q; // p, q all point to int type pointer


# Define pINT int * // occurs in the pre-compilation phase

Only simple macro replacement

Such as pINT p, q

Replace it with int * p, q; // p is the pointer, while q is only an integer variable.


Typedef is not a simple replacement

The description in MSDN is as follows:

Visual Studio 2010-Visual C ++ Typedef Declarations

A typedef declaration is a declaration with typedef as the storage class. the declarator becomes a new type. you can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared. typedef
Names allow you to encapsulate implementation details that may change.

A typedef declaration is interpreted in the same way as a variable or function declaration, but the identifier, instead of assuming the type specified by the declaration, becomes a synonym for the type.

Note that The declaration of The declarator becomes a new type. Note that it is a new type (1)

But what are the synonyms of becomes a synonym for the type? (I have not learned English well)


So can we use typedef INT int?

Then unsigned INT a = 1; // The answer is No if it is the same as unsigned int a = 1.

In this way, it cannot be passed in vc2010. In my understanding, the unsigned int type can also be written as unsigned,

Int Is also a type. The unsigned int type is not a combination of the unsigned type and int type, because int is signed and the former is unsigned.

INT is a new type (as mentioned above (1). An unsigned INT attempts to combine the two types will certainly cause an error.

Why can unsigned int be used? Because it is a data type, not a combination of the two data types.

As mentioned above, when typedef occurs during compilation, the compiler does not know the unsigned INT. It only knows the unsigned int.

# Define INT int

Unsigned INT Is simply replaced with unsigned int in the pre-compilation phase. In the compilation phase, the compiler sees that the unsigned int is of course no problem.

The key is to understand that the unsigned int is a data type, not a combination of the two data types unsigned and int.


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.