首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个c语言宏(#if)的有关问题

2012-03-01 
一个c语言宏(#if)的问题我是希望在编译阶段对两个结构体(A,B)的大小进行判断,如果A大于B就停止编译我试了

一个c语言宏(#if)的问题
我是希望在编译阶段对两个结构体(A,B)的大小进行判断,如果A大于B就停止编译
我试了下面这个宏

typedef   int   A;
typedef   char   B;
#if   sizeof(A)   >   sizeof(B)
#error   "A   can 't   bigger   than   B "
#endif
int   main()
{
                return   0;
}

编译参数如下
gcc   -c   tt.c   -Wall   -Wundef
出的错误如下
tt.c:3:5:   warning:   "sizeof "   is   not   defined
tt.c:3:11:   missing   binary   operator   before   token   "( "
gcc版本是gcc   version   3.4.6
请指教!

[解决办法]
#if constant-expression

The constant-expression is an integer constant expression with these additional restrictions:

Expressions must have integral type and can include only integer constants, character constants, and the defined operator.

The expression cannot use sizeof or a type-cast operator.

The target environment may not be able to represent all ranges of integers.

The translation represents type int the same as type long, and unsigned int the same as unsigned long.

The translator can translate character constants to a set of code values different from the set for the target environment. To determine the properties of the target environment, check values of macros from LIMITS.H in an application built for the target environment.

The expression must not perform any environmental inquiries and must remain insulated from implementation details on the target computer.

[解决办法]
你为什么一定要用#if呢,陷死胡同了。
#define STATIC_CHECK(exp) do{char staticChechError[(exp) ? 1 : -1]; }while(0);

STATIC_CHECK(sizeof(A) > sizeof(B));

热点排行