#ifndef #endif用法

Source: Internet
Author: User
Tags uppercase letter

One, do not overlook the #ifndef in the first piece, this is a very important thing. For example, you have two C files, all two C files include the same head file. At compile time, the two C files will be compiled together into a running file, so the problem is, a lot of declaration conflicts.


or the contents of the head file are placed in #ifndef and #endif. Whether or not your header file will be referenced by multiple files, you should add this. The general format is this:

#ifndef < logo >
#define < logo >
......
......

#endif

In theory it can be freely named, but the "logo" of each header file should be unique. The named rule for the identity is typically the header filename is all uppercase, underlined, and the "." in the file name. Also become underlined, such as: stdio.h

#ifndef _stdio_h_
#define _stdio_h_

......

#endif

Second, the most common form of conditional compilation commands is:
#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 ()
{
Charstr[20]= "C Language", C;
inti=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 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.

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.