如何检查宏中的常量值 [英] Howto check value of constants in a macro

查看:54
本文介绍了如何检查宏中的常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想定义几个常量并确保它们都比给定的其他常量小
。我认为这可以通过

简单宏来完成。这样的事情:


#define MAX 999

#define DEF_CHECKED_VAL(名称,值)#if(值< MAX)\

#define name MAX \

#else \

#define name value \

#endif


所以

DEF_CHECKED_VAL(test_1,33)应扩展为#define test_1 33



DEF_CHECKED_VAL(test_2,1000)应该产生#define test_2 999

你可能知道(我今天学到的)这不起作用。对于这个问题最常见的
相关的常见问题解答似乎是10.25我有这个棘手的

预处理我想做什么而且我不能想出一个这样做的方法。

不幸的是,编写我自己的预处理器并不是一个真正的选择,而且我不能相信我是第一个谁想要实现这个

功能。我的问题是否有一个众所周知的解决方案,或者我在做什么

从根本上说是愚蠢的东西?


感谢您的帮助,Rick

Hi,

I''d like to define several constants and make sure that all of them are
smaller than a given other constant. I thought this could be done by a
simple macro. Something like this:

#define MAX 999

#define DEF_CHECKED_VAL( name, value) #if (value < MAX) \
#define name MAX \
#else \
#define name value \
#endif

So
DEF_CHECKED_VAL(test_1, 33) should expand to #define test_1 33
and
DEF_CHECKED_VAL(test_2, 1000) should yield #define test_2 999
As you probably know (and I learned today) this doesn''t work. The most
relevant FAQ to this problems seems to be "10.25 I''ve got this tricky
preprocessing I want to do and I can''t figure out a way to do it."
Unfortunately writing my own preprocessor isn''t really an option and I
can''t believe that I''m the first one who''d like to implement this
functionality. Is there a well-known solution to my problem, or am I doing
something fundamentally stupid?

Thanks for any help, Rick

推荐答案

Richard Meister< r。******* @ t-online.de>写道:
Richard Meister <r.*******@t-online.de> writes:
我想定义几个常量,并确保它们都比给定的其他常量小。我认为这可以通过一个简单的宏来完成。这样的事情:

#define MAX 999
#define DEF_CHECKED_VAL(名称,值)#if(值< MAX)\
#define name MAX \\#
#else \
#define名称值\
#endif

所以
DEF_CHECKED_VAL(test_1,33)应扩展为#define test_1 33
和DEF_CHECKED_VAL(test_2,1000)应该产生#define test_2 999
I''d like to define several constants and make sure that all of them are
smaller than a given other constant. I thought this could be done by a
simple macro. Something like this:

#define MAX 999

#define DEF_CHECKED_VAL( name, value) #if (value < MAX) \
#define name MAX \
#else \
#define name value \
#endif

So
DEF_CHECKED_VAL(test_1, 33) should expand to #define test_1 33
and
DEF_CHECKED_VAL(test_2, 1000) should yield #define test_2 999




我认为上面的#if逻辑与你的例子相反,以便

<应该是> ;.


这是一个可能的解决方案:

#define DEF_CHECKED_VAL(名称,值)\

enum {name =(value)< MAX? (值):MAX};

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q ){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

) ;}返回0;}



I think the #if logic above is opposite your examples, so that
the < should be >.

Here is one possible solution:
#define DEF_CHECKED_VAL(name, value) \
enum { name = (value) < MAX ? (value) : MAX };
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Ben Pfaff< bl*@cs.stanford.edu>写道:
Ben Pfaff <bl*@cs.stanford.edu> writes:
Richard Meister< r。******* @ t-online.de>写道:
Richard Meister <r.*******@t-online.de> writes:
我想定义几个常量,并确保它们都比给定的其他常量小。我认为这可以通过一个简单的宏来完成。这样的事情:

#define MAX 999
#define DEF_CHECKED_VAL(名称,值)#if(值< MAX)\
#define name MAX \\#
#else \
#define名称值\
#endif

所以
DEF_CHECKED_VAL(test_1,33)应扩展为#define test_1 33
和DEF_CHECKED_VAL(test_2,1000)应该产生#define test_2 999
I''d like to define several constants and make sure that all of them are
smaller than a given other constant. I thought this could be done by a
simple macro. Something like this:

#define MAX 999

#define DEF_CHECKED_VAL( name, value) #if (value < MAX) \
#define name MAX \
#else \
#define name value \
#endif

So
DEF_CHECKED_VAL(test_1, 33) should expand to #define test_1 33
and
DEF_CHECKED_VAL(test_2, 1000) should yield #define test_2 999



我认为上面的#if逻辑与你的例子相反,所以< <<应该是> ;.

这是一个可能的解决方案:
#define DEF_CHECKED_VAL(名称,值)\
枚举{name =(value)< MAX? (值):MAX};



I think the #if logic above is opposite your examples, so that
the < should be >.

Here is one possible solution:
#define DEF_CHECKED_VAL(name, value) \
enum { name = (value) < MAX ? (value) : MAX };




哦,这很聪明!


如果你需要更复杂的东西,不能用

整数值和常量表达式表示,你总是可以实现你的* b $ b自己的* simple *预处理器。没有必要编写一个完全的

功能的C预处理器,只需要一个产生C代码的程序作为其

输出。


例如,给定这样的输入:


MAX 999

DEF_CHECKED_VAL test_1 33

DEF_CHECKED_VAL test_2 1000


程序可以产生以下输出:


#define test_1 33

#define test_2 999


在构建过程中,您可以运行程序并生成一个

头文件,然后由C源文件包含#included。定义

您喜欢的转换语法和语义;

越简单,就越容易实现。


(就我个人而言,我会使用Perl这样的东西,但它可以

当然可以在C中完成。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Oooh, that''s clever!

If you need something more complex, that can''t be expressed with
integer values and constant expressions, you can always implement your
own *simple* preprocessor. There''s no need to write a fully
functional C preprocessor, just a program that produces C code as its
output.

For example, given an input like this:

MAX 999
DEF_CHECKED_VAL test_1 33
DEF_CHECKED_VAL test_2 1000

the program could produce the following output:

#define test_1 33
#define test_2 999

In your build procedure, you can run your program and generate a
header file that''s then #included by your C source files. Define
whatever syntax and semantics you like for the tranformation; the
simpler it is, the easier it will be to implement.

(Personally, I''d use Perl for something like this, but it can
certainly be done in C.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Richard Meister写道:
Richard Meister wrote:



我'我想定义几个常数,并确保它们都比给定的其他常数小。我认为这可以通过一个简单的宏来完成。

Hi,

I''d like to define several constants and
make sure that all of them are
smaller than a given other constant. I thought this could be done by a
simple macro.




我使用断言宏。


#include< assert.h>


void assert(标量表达式);


-

皮特



I use the assert macro for that.

#include <assert.h>

void assert(scalar expression);

--
pete


这篇关于如何检查宏中的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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