ç$ P $#如果pprocessor前pression问题 [英] C preprocessor #if expression question

查看:128
本文介绍了ç$ P $#如果pprocessor前pression问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对前pression我们可以在C语言与#IF preprocessor使用的类型有点糊涂。我尝试以下code和它不工作。请解释和可与preprocessor使用前pressions提供范例

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&stdlib.h中GT;
INT C = 1;#如果Ç== 1
#定义检查(一)款(a == 1):5
#定义TABLE_SIZE 100
#万一
诠释的main()
{
诠释一个= 0,B;
的printf(a =%d个\\ N,一);
B =检查(一);
输出(A =%D \\ n,一,TABLE_SIZE);
系统(暂停);
返回0;
}


解决方案

在preprocessor不能使用变量来自前pressions C程序 - 它只能在preprocessor宏行事。所以,当你尝试在preprocessor使用 C 你没有得到你所期望的。

不过,你也别因为当preprocessor尝试计算未被定义为宏的标识符,它把标识符作为具有零价值得到一个错误。

所以,当你打这个片断:

 #如果Ç== 1
#定义检查(一)款(a == 1):5
#定义TABLE_SIZE 100
#万一

C 使用的preprocessor无关的变量 C 从C程序。在preprocessor看起来,看看是否有公司的C 为定义的宏。既然没有,则计算如下EX pression:

  0#如果== 1

这是假的,当然。

既然你似乎没有使用变量 C 在程序中,你可以做以下获得一致的行为与你想什么:

 #定义的C 1#如果Ç== 1
#定义检查(一)款(a == 1):5
#定义TABLE_SIZE 100
#万一

(请注意,我也做了与惯例宏名保持大写宏名。)

I am a bit confused on the type of expression we can use with #IF preprocessor in c language. I tried the following code and it isn't working. Please explain and provide examples for expressions that can be used with the preprocessor.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int c=1;

#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif
int main()
{
int a=0,b;
printf("a=%d\n",a);
b=check(a);
printf("a=%d %d\n",a,TABLE_SIZE);
system("PAUSE");
return 0;
}

解决方案

The preprocessor cannot use variables from the C program in expressions - it can only act on preprocessor macros. So when you try to use c in the preprocessor you don't get what you might expect.

However, you also don't get an error because when the preprocessor tries to evaluate an identifier that isn't defined as a macro, it treats the identifier as having a value of zero.

So when you hit this snippet:

#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif

The c used by the preprocessor has nothing to do with the variable c from the C program. The preprocessor looks to see if there's a macro defined for c. Since there isn't, it evaluates the following expression:

#if 0==1

which is false of course.

Since you don't appear to use the variable c in your program, you can do the following to get behavior in line with what you're trying:

#define C 1

#if C==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif

(Note that I also made the macro name uppercase in keeping with convention for macro names.)

这篇关于ç$ P $#如果pprocessor前pression问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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