本教程解释了如何在 C 语言中使用 #warning 预处理器指令。更多C教程请访问码农之家
描述
在 C 编程语言中,#warning 指令类似于#error 指令,但不会导致取消预处理。#warning 指令之后的信息在预处理继续之前作为消息输出。
语法
C 语言中#warning 指令的语法是:
#warning message
message 在继续预处理之前要输出的消息。
例子
让我们看看如何在 C 程序中使用#warning 指令。
以下示例显示了 #warning 指令的输出:
/* Example using #warning directive by TechOnTheNet.com */
#include <stdio.h>
int main()
{
/* The age of TechOnTheNet in seconds */
int age;
#warning The variable age may exceed the size of a 32 bit integer
/* 12 years, 365 days/year, 24 hours/day, 60 minutes/hour, 60 seconds/min */
age = 12 * 365 * 24 * 60 * 60;
printf("TechOnTheNet is %d seconds old\n", age);
return 0;
}
编译这个程序时,预处理器会输出如下警告:
warning: The variable age may exceed the size of a 32 bit integer
由于这是一个#warning 指令,程序编译继续,我们可以执行程序来查看它的输出。
这是可执行程序的输出:
TechOnTheNet is 378432000 seconds old