Macro definition error in C?

2019-02-25 14:19发布

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

It complains error: expected expression before '/' token. Was is it bacause I can't use macro to define decimals or what?

3条回答
地球回转人心会变
2楼-- · 2019-02-25 14:57

You're using C, but you're trying to use a C++ style // comment. Depending on your compiler, that may not be allowed.

Edit: In fact, gcc -c89 -ansi gives that exact error message for a // comment and a totally different one for the extraneous ; in the define.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-25 15:07
#define SOUND_SPEED 0.034;
                         ^

Do not use the trailing ;

Actually you should never terminate a macro with a ;:

PRE11-C. Do not conclude macro definitions with a semicolon https://www.securecoding.cert.org/confluence/display/seccode/PRE11-C.+Do+not+conclude+macro+definitions+with+a+semicolon

查看更多
smile是对你的礼貌
4楼-- · 2019-02-25 15:21

Drop the semicolon:

#define SOUND_SPEED 0.034; 
                         ^

If you keep it the generated code will look like this:

distance = (double)(rtt*SOUND_SPEED;)/2;
                                   ^
查看更多
登录 后发表回答