preprocessor ​​#if指令 [英] Preprocessor #if directive

查看:120
本文介绍了preprocessor ​​#if指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个大code和我不希望这一切在我的main.c所以我写了一个具有if-else语句与功能的。公司文件,我不知道能不能写像这样的:

I am writing a big code and I don't want it all to be in my main.c so I wrote a .inc file that has IF-ELSE statement with function and I was wondering can it be written like this:

#if var==1
process(int a)
{
    printf("Result is: %d",2*a);
}
#else
process(int a)
{
    printf("Result is: %d",10*a);
}
#endif

我试图编译它,但它给了我错误,或者在最好的情况下它只是第一个函数的处理的不检查var变量(它被设置为0)。

I tried to compile it but it gives me errors or in best case it just goes on the first function process without checking the var variable (it is set to 0).

推荐答案

在preprocessor不知道任何变量的值,因为它甚至其编译之前,而不是在运行时的工作。

The preprocessor doesn't "know" the value of any variable, because it does its work even before compilation, not at runtime.

在一个preprocessor条件#如果您只能评估的#define 'D符号和恒恩pressions。

In the condition of a preprocessor #if you can only evaluate #define'd symbols and constant expressions.

你是显示的具体例子可以简单地转换为:

The particular example you are showing can be simply converted to:

printf("Result is: %d", (var == 1 ? 2: 10) * a);

这篇关于preprocessor ​​#if指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆