Explanation of #define followed by only one identifier

Publisher:世界因你而精彩Latest update time:2015-08-11 Source: dzscKeywords:define Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In general, all lines in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, specify the compilation conditions for a part of the content, which is "conditional compilation". Sometimes, you want to compile a group of statements when a certain condition is met, and compile another group of statements when the condition is not met.
The most common form of conditional compilation command is:
#ifdef identifier
Program segment 1
#else
program segment 2
#endif
Its function is: when the identifier has been defined (usually defined by the #define command), compile program segment 1, otherwise compile program segment 2.
The #else part can also be omitted, that is:
#ifdef
program segment 1
#denif
The "program segment" here can be a statement group or a command line. This conditional compilation can improve the versatility of C source programs. If a C source program runs on different computer systems, and different computers have certain differences. For example, we have a data type that should be represented by long type in Windows platform and float in other platforms. This often requires necessary modifications to the source program, which reduces the versatility of the program. The following conditional compilation can be used:
#ifdef WINDOWS
#define MYTYPE long
#else
#define MYTYPE float
#endif
If you compile a program on Windows, you can add
#define WINDOWS at the beginning of the program
. Then the following command line will be compiled:
#define MYTYPE long
If the following command line appears before this set of conditional compilation commands:
#define WINDOWS 0
Then MYTYPE in the pre-compiled program will be replaced by float. In this way, the source program can be used on different types of computer systems without any modification. Of course, the above introduction is just a simple case. Other conditional compilations can be designed based on this idea.
For example, when debugging a program, you often want to output some required information, but no longer output this information after debugging. You can insert the following conditional compilation section in the source program:
#ifdef DEBUG
print ("device_open(%p)n", file);
#endif
If there is the following command line before it:
#define DEBUG
Then the value of the file pointer will be output when the program is running for debugging and analysis. After debugging, just delete this define command line. Some people may think that this goal can be achieved without conditional compilation, that is, add a batch of printf statements during debugging, and delete the printf statements one by one after debugging. Indeed, this is possible. However, when there are many printf statements added during debugging, the workload of modification is very large. With conditional compilation, there is no need to delete and modify the printf statements one by one. Just delete the previous "#define DEBUG" command. At this time, all conditional compilation segments using DEBUG as an identifier will make the printf statements in them ineffective, that is, they play a unified control role, just like a "switch".
Sometimes the following form is also used:
#ifndef identifier
Program segment 1
#else
program segment 2
#endif
Only the first line is different from the first form: "ifdef" is changed to "ifndef". Its function is: if the identifier is not defined, compile program segment 1, otherwise compile program segment 2. This form has the opposite effect of the first form.
The above two forms are similar in usage. Choose one according to your needs and it depends on your convenience.
There is another form, that is, #if is followed by an expression instead of a simple identifier:
#if expression
Program segment 1
#else
program segment 2
#endif
Its function is: when the value of the specified expression is true (non-zero), program segment 1 is compiled, otherwise program segment 2 is compiled. Certain conditions can be given in advance to make the program perform different functions under different conditions.
For example: input a line of letters, set conditional compilation as needed, so that all letters can be changed to uppercase or lowercase letters for 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 running result is: C LANGUAGENow
define LETTER as 1. In this way, when the conditional compilation command is preprocessed, since LETTER is true (non-zero), the first if statement is compiled, and the lowercase letters are changed to uppercase during runtime. If the first line of the program is changed to:
#define LETTER 0
, the second if statement is compiled during preprocessing, and the uppercase letters are changed to lowercase (the difference between the ASCII code of the uppercase letter and the corresponding lowercase letter is 32). At this time, the running status is:
c language
Someone may ask: If we can achieve the requirement by directly using if statements without conditional compilation commands, what is the benefit of using conditional compilation commands? Indeed, this problem can be solved without conditional compilation, but the target program will be longer (because all statements are compiled). By using conditional compilation, the number of compiled statements can be reduced, thereby reducing the target program length. When there are many conditional compilation sections, the target program length can be greatly reduced. _______________________________________________________________________________________

#ENDIF
#define __CONFIG_H
There is only an identifier after #define, but no replaced number. Can anyone explain what this means?

You are talking about the file in .h. There should be a #endif at the end, which is written to avoid the .h file from being redefined. For example, AC includes BH and CH, and BH includes CH. When AC is compiled, CH will be compiled repeatedly. When
#ifndef __CONFIG_H is written before CH,
#ifndef __CONFIG_H
#ifndef __CONFIG_H will not pass because it has been defined before. It avoids redefinition.


To summarize:
Use #ifndef to prevent duplicate definitions. It is just a macro definition, which is mostly used in exclusive definitions in C language. The most common usage is to completely encapsulate the entire public header file to avoid nested references and duplicate definitions. The general usage is as follows:
#ifndef __CONFIG_H
#define __CONFIG_H
header file body
#define __CONFIG_H

Keywords:define Reference address:Explanation of #define followed by only one identifier

Previous article:What does code mean in C language of microcontroller?
Next article:Serial communication working mode 1 baud rate calculation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号