#if常量表达式 [英] #if constant expression
问题描述
根据C90的6.8,#if需要不断表达。
根据C90的6.4,sizeof运算符是常量的一部分
表达。
为什么我的大部分编译器都会这样做?
#include< stdio.h>
#if sizeof(int)> = 4
#define XXX" big"
#else
#define XXX" small"
#endif
int main(无效)
{
printf(" hello%s \ nn",XXX);
返回(0);
}
他们对#if sizeof ...进行了抨击。行。
他们明确表示我不能在#if
中使用sizeof所以我错过了哪一部分标准?
BFN。保罗。
According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
Why then do most of my compilers barf on this?
#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
They barf on the "#if sizeof ..." line.
They explicitly say that I can''t use sizeof in a #if
So which bit of the standard did I miss?
BFN. Paul.
推荐答案
#include< stdio.h>
#include <stdio.h>
>
#if sizeof(int)> = 4
#define XXX" big" ;
#else
#define XXX" small"
#endif
int main (无效)
{
printf(" hello%s \ n",XXX);
return(0);
}
>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
[code snnipet ...]
[code snnipet...]
我错过了哪个标准?
So which bit of the standard did I miss?
嗯,我认为#if只能用于宏(因为sizeof而在编译时将
替换为带有表达式的表达式)得到
在运行时解决我不认为使用sizeof是好主意
#if !!
问候
Vikas Gupta
Well I think that #if can be used only with the Macros(who gets
replaced with contant expressions at compile time) since sizeof gets
resolved at run time I dont think it''s good idea to use sizeof with
#if!!
Regards
Vikas Gupta
kerravon写道:
kerravon wrote:
根据C90的6.8 ,#if需要一个恒定的表达式。
根据C90的6.4,sizeof运算符是常量
表达式的一部分。
为什么我的大部分编译器都会这样做?
#include< stdio.h>
# if sizeof(int)> = 4
#define XXX" big"
#else
#define XXX" small"
#endif
int main(无效)
{
printf(" hello%s \ n",XXX);
返回(0);
}
他们关于#if sizeof ...的barf行。
他们明确表示我不能在#if
中使用sizeof所以我错过了哪一部分标准?
BFN。保罗。
According to 6.8 of C90, #if takes a constant expression.
According to 6.4 of C90, the sizeof operator is part of a constant
expression.
Why then do most of my compilers barf on this?
#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}
They barf on the "#if sizeof ..." line.
They explicitly say that I can''t use sizeof in a #if
So which bit of the standard did I miss?
BFN. Paul.
我没有C90的副本;但我怀疑这不是一个区域,其中C90和C99之间的差异很重要。
n1256.pdf的第6.10.1p4节说
"由于宏扩展和*定义*一元的所有替换后
$ b已执行$ b运算符,所有剩余的标识符(包括与关键字词汇相同的那些
)将替换为pp-number 0,
I相信这适用于你的例子中''sizeof''和'int''。
结果,它变成:
#if 0(0)> = 4
解释了它失败的原因。
I don''t have a copy of C90; but I suspect that this is not an area where
the differences between C90 and C99 matter. Section 6.10.1p4 of
n1256.pdf says
"After all replacements due to macro expansion and the *defined* unary
operator have been performed, all remaining identifiers (including those
lexically identical to keywords) are replaced with the pp-number 0, "
I believe that this applies to both ''sizeof'' and ''int'' in your example.
As a result, it becomes:
#if 0(0) >= 4
which explains why it fails.
vicks< vi ***** **@gmail.com在新闻中写道:d485a4a6-dd4a-492e-a00b-
9b ** ********@r1g2000hsg.googlegroups.com :
vicks <vi*******@gmail.comwrote in news:d485a4a6-dd4a-492e-a00b-
9b**********@r1g2000hsg.googlegroups.com:
我认为#if只能用于宏(谁得到
在编译时用繁琐的表达式替换)因为sizeof得到
在运行时解析我不认为它是'g ood想要使用sizeof和
#if !!
Well I think that #if can be used only with the Macros(who gets
replaced with contant expressions at compile time) since sizeof gets
resolved at run time I dont think it''s good idea to use sizeof with
#if!!
sizeof产生编译时常量,并且在
运行时非常不被评估。
-
$ b $bTomásóhéilidhe
sizeof yields a compile-time constant, and is very much _not_ evaluated at
runtime.
--
Tomás ó héilidhe
这篇关于#if常量表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!