Explanation # ifdef and # ifndef

Source: Internet
Author: User

These macros are used for Conditional compilation. GenerallyProgramAll rows are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, to specify the compilation conditions for a part of the content. This is "Conditional compilation ". Sometimes, you want to compile a group of statements when a condition is met, and compile another group of statements when the condition is not met. The most common form of Conditional compilation commands is:

# 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 segment 1 # denif the "segment" here 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 between different computers. For example, we have a data type. In Windows, we should use the long type, while in other platforms, we should use the float representation. In this case, we often need to make necessary modifications to the source program, this reduces the versatility of the program. You can compile the program using the following conditions: # ifdef windows # define mytype long # else # define mytype float # endif if you compile the program on Windows, you can add # define windows at the beginning of the program to compile the following command line: # define mytype long if the following command line appears before this set of Conditional compilation commands: # define windows 0, the mytype in the pre-compiled 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 a simple case. 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 ); # If endif has the following command line before it: # define debug, the file pointer value is output during the program running for debugging and analysis. After debugging, you only need to delete the define command line. Some people may think that Conditional compilation can achieve this goal, that is, adding a batch of printf statements during debugging, and deleting the printf statements one by one after debugging. Indeed, this is acceptable. However, when many printf statements are added during debugging, the modification workload is huge. If you use Conditional compilation, you do not need to delete the printf statement one by one. You only need to delete the previous "# define debug" command, at this time, all the Conditional compilation segments using debug as identifiers make the printf statement ineffective, that is, unified control, just like a "Switch. Sometimes the following format is used: # ifndef identifier segment 1 # else Segment 2 # endif is only the first line and 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 form 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. Another form is: # If is followed by an expression instead of a simple identifier: # If expression Section 1 # else section 2 # endif: if the specified expression value is true (non-zero), program segment 1 is compiled; otherwise, program segment 2 is compiled. You can specify certain conditions in advance so that the program can execute different functions under different conditions. For example, enter a line of letters, set the condition for compiling as needed, so that all letters can be changed to uppercase for output, or all letters can be changed to lowercase for output. # Define Letter 1 main () {char STR [20] = "C Language", C; int I = "0"; while (C = STR [I])! = '\ 0') {I ++; # If letter if (C> = 'A' & C <= 'Z') C = "c-32 "; # else if (C> = 'A' & C <= 'Z') C = "C" + 32; # endif printf ("% C ", c) ;}} the running result is: C language first defines letter as 1. In this way, when compiling commands with preprocessing conditions, the letter is true (non-zero ), compile the first if statement, and change the lower-case letters to the upper-case letters during running. If you change the first line of the program to: # define letter 0, compile the second if statement during preprocessing, converts an upper-case letter to a lower-case letter (ASCII of an upper-case letter and the corresponding lower-case letter)Code 32 ). In this case, the running status is C language. Some people may ask: The if statement can be used directly without the Conditional compilation command to meet the requirements. What are the advantages of the Conditional compilation command? Indeed, this problem can be solved without the need for Conditional compilation. However, if the target program is long (because all statements are compiled), Conditional compilation can reduce compiled statements, this reduces the length of the target. When there are many Conditional compilation segments, the length of the target program can be greatly reduced.

# Ifdef # ifndef and other usage

# Ifndef in the header, which is critical. For example, you have two C files, both of which include the same header file. During compilation, these two C files need to be compiled into a runable file together, so the problem arises and a large number of declarations conflict.

Put the header file content in # ifndef and # endif. Whether or not your header file will be referenced by multiple files, you must add this. The general format is as follows:

# Ifndef <ID> # define <ID>

............

# Endif

<Identifier> in theory, it can be freely named, but the "identifier" of each header file should be unique. The naming rule of the identifier is generally that the header file name is in uppercase, followed by an underscore, and the "." In the file name is also changed to an underscore, such as stdio. h.

# Ifndef _ stdio_h _ # DEFINE _ stdio_h _

......

# Endif

2. Problems with defining variables in # ifndef (generally not defined in # ifndef ).

# Ifndef AAA # define aaa... int I ;... # An I repeated definition error occurs when the variable definition in endif is linked in VC, and compilation is successful in C.

Conclusion:

(1 ). when you first use this header. CPP file generation. in OBJ, int I defines in it when another one uses this. CPP is generated [separately] again. when obj is used, int I is defined again, and then two OBJ are another. CPP also includes this header. If the header is connected together, the definition will be repeated.

(2). After the source program file extension is changed to. C, VC compiles the source program according to the C language syntax, rather than C ++. In C language, if multiple int I is encountered, one of them is automatically considered to be the definition, and the other is the declaration.

(3). The connection results of C and C ++ are different. during compilation, C ++ may set the global variable as a strong symbol by default. Therefore, the connection error occurs. The C language determines the strength based on the initialization. (Reference)

Solution:

(1). Change the source program file extension to. C.

(2). Recommended Solution: Only extern int I is declared in. h; defined in. cpp

# Ifndef _ x_h __

# DEFINE _ x_h __

Extern int I;

# Endif/_ x_h _ int I;

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.