# If # ifdef and # ifndef usage and difference

Source: Internet
Author: User

# If # ifdef and # ifndef usage

The priority of the shift operator is higher than that of the conditional operator, and the heavy load operator cannot change the operator priority. Therefore, the code should be adjusted as follows. When writing a macro, pay attention to the priority, use parentheses to block operator priority.
# Define MAXIMUM (x, y) (x)> (y )? (X) :( y ))
# Define MINIMUM ....

Instance

The code is as follows: Copy code

# Include <stdio. h>
 
# Define MAX
 
# Define MAXIMUM (x, y) x> y? X: y
# Define MINIMUM (x, y) x <y? X: y
 
Int main ()
 {
Int a = 10, B = 20;
 
# Ifdef MAX
Printf ("40: The larger one is % dn", MAXIMUM (a, B ));
# Else
Printf ("40: The lower one is % dn", MINIMUM (a, B ));
# Endif
 
 
# Ifndef MIN
Printf ("40: The lower is % dn", MINIMUM (a, B ));
# Else
Printf ("40: The larger one is % dn", MAXIMUM (a, B ));
# Endif
 
# Undef MAX
# Ifdef MAX
Printf ("40: The larger one is % dn", MAXIMUM (a, B ));
# Else
Printf ("40: The lower one is % dn", MINIMUM (a, B ));
# Endif
 
 
# Define MIN
# Ifndef MIN
Printf ("40: The lower one is % dn", MINIMUM (a, B ));
# Else
Printf ("40: The larger one is % dn", MAXIMUM (a, B ));
# Endif
Return 0;
 
 }

# If # difference between ifdef and # ifndef

# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif

It is used to compile program segment 1 when the identifier has been defined (generally defined using the # define Command). Otherwise, compile program segment 2.
The # else part can also be absent, I .e:
# Ifdef
Procedure 1
# Denif

The "program segment" can be a statement group or a command line. This conditional compilation can improve the universality of the C source program. If a C source program runs on different computer systems
There are some differences with computers. For example, we have a data type. In Windows, we should use the long type, while in other platforms, we should use float.
It is often necessary to make necessary modifications to the source program, which reduces the universality of the program. You can compile with the following conditions:
# Ifdef WINDOWS
# Define MYTYPE long
# Else
# Define MYTYPE float
# Endif

If you compile a program on Windows, you can add
# Define WINDOWS

In this way, compile the following command line:
# Define MYTYPE long

If the following command line appears before this set of conditional compilation commands:
# Define WINDOW

After pre-compilation, The MYTYPE in the program is replaced by float. In this way, the source program can be used in different types of computer systems without any modification. Of course, the above is just
In simple cases, you can design other conditions for compilation based on this idea.
For example, when debugging a program, you often want to output some required information, but do not output this information after the debugging is complete. You can insert the following conditional compilation segments in the source program:
# Ifdef DEBUG
Print ("device_open (% p) n", file );
# Endif

If you have the following command line before it:
# Define DEBUG

The value of the file pointer is output when the program is running for debugging and analysis. After debugging, you only need to delete the define command line. Some people may think that conditional compilation is also possible.
Add a batch of printf statements during debugging. After debugging, delete the printf statements one by one. Indeed, this is acceptable. However, when many printf statements are added during debugging, the modified
The workload is huge. With conditional compilation, you do not need to delete the printf statement one by one. You only need to delete the previous "# define DEBUG" command. In this case, all
The conditional compilation segments of identifiers make the printf statement ineffective, that is, unified control, just like a "switch.

The following format is also used:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif

The first line is different from the first form: change "ifdef" to "ifndef ". It is used to compile program segment 1 if the identifier is not defined; otherwise, compile program segment 2. This shape
Is opposite to the first form.
The usage of the above two forms is similar. You can choose one as needed, depending on your convenience.

**************************************** * ********************************** & Shy; **********************************
Another form is: # if is followed by an expression instead of a simple identifier:
# If expression
Procedure 1
# Else
Procedure 2
# Endif

It is used to compile program segment 1 when the specified expression value is true (non-zero); otherwise, compile program segment 2. Certain conditions can be specified in advance so that the program can execute different functions under different conditions.
Yes.

 

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.