概述
The #if ... #else is a preprocessor directive in C programming language and it is used for conditional compilation, where one of the code section needs to be compiled based on the condition from two given code sections.
#if ... #else是C编程语言中的预处理程序指令,用于条件编译,其中需要根据两个给定代码段中的条件来编译其中一个代码段。
General for of the #if ... #else directive is:
#if ... #else指令的一般含义是:
#if conditional_expression
statements-true;
#else
statements-false;
#endif
If conditional_expression is true, the code written between in #if block i.e. statements-true will be executed and if conditional_expression is false, code written in #else block i.e. statements-false will be executed.
如果conditional_expression为true,则将执行在#if块(即statement-true)之间编写的代码,如果conditional_expression为false,则将执行在#else块(即statements-false)中编写的代码。
Example:
例:
#include <stdio.h>
#define MAX_GF 20
int main(){
printf("Hellon");
#if MAX_GF>=50
printf("Wau, you may have more than 50 girlfriendsn");
printf("Code for more than 50 GFsn");
#else
printf("Wau, you may not have more than 50 girlfriendsn");
printf("Code for less than 50 GFsn");
#endif
printf("Bye!n");
return 0;
}
Output
输出量
Hello
Wau, you may not have more than 50 girlfriends
Code for less than 50 GFs
Bye!
翻译自: https://www.includehelp.com/c-programs/if-else-directive-example.aspx
最后
以上就是健康唇膏为你收集整理的#if ... #else指令C语言中的示例| C预处理程序的全部内容,希望文章能够帮你解决#if ... #else指令C语言中的示例| C预处理程序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复