C C + + is the King began to learn the basis of C #ifndef, #def, #endif说明

Source: Internet
Author: User
Tags lowercase uppercase letter
The macros you encounter are for conditional compilation. In general, all rows in the source program participate in the compilation. But sometimes it is desirable to compile a portion of the content only if it satisfies a certain condition, that is, to specify the condition of compilation for a portion of the content, which is conditional compilation. Sometimes, you want to compile a set of statements when a condition is met, and compile another set of statements when the condition is not satisfied.
The most common forms of conditional compilation commands are:
#ifdef identifier
Program Section 1
#else
Program Section 2
#endif

Its function is: When the identifier has been defined (generally with the #define command definition), the program Segment 1 is compiled, or the compiler section 2.
Where the #else part can also not be, namely:
#ifdef
Program Section 1
#denif

The "program segment" Here can be a statement group, or it can be a command line. This kind of conditional compilation can improve the generality of C source program. If a C source program is running on different computer systems, there is a difference between the different computers. For example, we have a data type that, in a Windows platform, should be represented by a long type, and float should be used on other platforms, which often requires necessary modifications to the source program, which reduces the versatility of the program. You can compile with the following criteria:
#ifdef WINDOWS
#define MYTYPE Long
#else
#define MYTYPE Float
#endif

If you are compiling a program on Windows, you can add it at the beginning of the program
#define WINDOWS

This compiles the following command line:
#define MYTYPE Long

If the following command line appears before this set of conditions compiles the command:
#define WINDOWS 0

Then the MyType in the precompiled program is replaced with float. In this way, the source program can be used for different types of computer systems without making any changes. Of course, the above introduction is only a simple situation, can be based on this idea to design other conditional compilation.
For example, when you are debugging a program, you often want to output some of the information you need, and you will not output the information until 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 in front of it:
#define DEBUG

The value of the file pointer is output when the program is run to debug the analysis. Just remove the Define command line when debugging is complete. Some people may feel that the use of conditional compilation can also achieve this goal, that is, when debugging a batch of printf statements, after debugging will be the printf statement deleted. Indeed, it is possible. However, when debugging the addition of printf statements for a long time, the amount of work to modify is very large. With conditional compilation, you do not have to one by one delete printf statements, just remove one of the previous "#define DEBUG" command, all of which use DEBUG as an identifier of the conditional compilation segment makes the printf statement does not work, that is, the role of unified control, as a "switch".
Sometimes the following form is used:
#ifndef identifier
Program Section 1
#else
Program Section 2
#endif

Only the first line differs from the first: "Ifdef" is changed to "Ifndef". Its function is to compile program segment 1 if the identifier is not defined, or to compile program segment 2. This form is contrary to the function of the first form.
The above two forms of usage are similar, choose one according to the need, depending on the convenience.
Another form is that #if is followed by an expression, not a simple identifier:
#if an expression
Program Section 1
#else
Program Section 2
#endif

The function is to compile program segment 1 when the specified expression value is true (not 0), or else compile the program segment 2. A certain condition can be given beforehand to enable the program to perform different functions under different conditions.
For example: Enter a line of alphabetic characters, set conditional compilation as needed, so that it can change the letter to uppercase output, or change it to lowercase output.
#define LETTER 1
Main ()
{
Char str[20]= "C Language", C;
int i=0;
while ((c=str[i))!= ' ") {
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 results of the operation are: C LANGUAGE
Now define letter 1 so that when the preprocessing condition compiles the command, because letter is true (not 0), the first if statement is compiled, and the runtime causes lowercase letters to be capitalized. If you change the first line of the program to:
#define LETTER 0

When preprocessing, the second if statement is compiled and the uppercase letter is lowercase (the ASCII code for uppercase letters and corresponding lowercase letters is 32 less). The operation at this time is:
C language
Some people will ask: without the conditional compilation command and directly with the IF statement can achieve the requirements, the use of conditional compilation command what is the advantage. Indeed, this problem can be completely done without conditional compilation, but the target program is long (because all statements are compiled), and conditional compilation allows you to reduce the compiled statements, thereby reducing the length of the target. When the conditional compilation segment is more, the target program length can be greatly reduced.
Related Article

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.