最近在看预编译的问题,刚刚看到了类似这样的程序,但是却不知道为什么不能运行。
#include <stdio.h>
#define DEBUG
#ifdef DEBUG
printf("This is the Debug.\n");
#endif
void main()
{
printf("Hello World!\n");
}
这个代码的意思我猜应该是如果我定义了DEBUG那么就会输出This is the DEBUG和Hello World,如果没定义DEBUG那么就只输出Hello World。求解。
c语言从语言层面来讲,必须从main函数开始所以你再main函数之前调用printf是不被允许的,可以改成如下方式:
#include <stdio.h>
#define DEBUG
void main()
{
#ifdef DEBUG
printf("This is the Debug.\n");
#endif
printf("Hello World!\n");
}
#define DEBUG
#ifdef DEBUG
printf("This is the Debug.\n");
#endif 放在函数里
否则这一句代码被丢在了任何函数之外,这是不符合C语言语法的