# Define typedef and # ifdef # define # ifndef # endif

Source: Internet
Author: User

1. # difference between define and typedef

# Define is generally used to define constants and statement macro definitions and act as macro replacement during compilation. Of course, you can also complete a small part of the typedef function. Remember that it is only a small part.

Typedef is used to declare a new type name for readability and variable definition.

Example 1:

# Define int // No semicolon !!

Typedef int; // remember that there is no # Before typedef, and this statement is required. The semicolon ends.

In this example, int num; can be implemented in this way, basically no difference.

Example 2:

# Define pint int *

Typedef int * pint;

There is a difference in this example. We need to define int * a, * B; Use define to implement pint a, B; replace int * a, B; it defines pointer A and variable B. In typedef usage, pint A and B define pointer variable A and pointer variable B.

Therefore, typedef must be used to declare a new type name for security purposes.

 

2. # define, # If defined (), # ifndef, # ifdef, # else # elseif # endif

A) # define defines constants. For example, # define debug indicates that debug has been defined.

B) # If defined (Debug), if there is only one condition, it is equivalent to # ifdef debug

Therefore, # If defined () is often used in multi-condition environments. You can use "|, &,!" in the middle of multiple conditions ,&&,!" For example, # If defined (ebug) & defined (win32)

This means that when both Debug and Win32 are defined, the statement under the # If condition is entered.

C) # ifdef If debug is defined, execute the statement under the # ifdef condition. Its intention and # ifndef debug

On the contrary, N in the middle indicates that there is no meaning, that is, if there is no definition ....

D) # endif pairing # If pairing

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fangjuntan/archive/2009/06/07/4248861.aspx

 

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.