C中的宏定义错误?

时间:2012-03-01 21:58:37

标签: c macros c-preprocessor

#define SOUND_SPEED 0.034;    
int rtt; //round trip time in microsecond
double distance;
distance = (double)(rtt*SOUND_SPEED)/2;

它抱怨错误:'/'标记之前的预期表达式。是因为我不能使用宏来定义小数或什么?

3 个答案:

答案 0 :(得分:8)

删掉分号:

#define SOUND_SPEED 0.034; 
                         ^

如果保留它,生成的代码将如下所示:

distance = (double)(rtt*SOUND_SPEED;)/2;
                                   ^

答案 1 :(得分:3)

#define SOUND_SPEED 0.034;
                         ^

请勿使用尾随;

实际上,您永远不应该使用;终止宏:

  

PRE11-C。不要用分号结束宏定义           https://www.securecoding.cert.org/confluence/display/seccode/PRE11-C.+Do+not+conclude+macro+definitions+with+a+semicolon

答案 2 :(得分:1)

您正在使用C,但您正在尝试使用C ++样式//注释。根据您的编译器,可能不允许这样做。

编辑:事实上,gcc -c89 -ansi为//评论提供确切的错误消息,并为定义中无关的;提供完全不同的评论。