Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro definition error in C?

#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?

like image 767
Kevin Q Avatar asked May 04 '26 02:05

Kevin Q


2 Answers

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;
                                   ^
like image 86
cnicutar Avatar answered May 05 '26 19:05

cnicutar


#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

like image 41
ouah Avatar answered May 05 '26 17:05

ouah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!