Why #define INVALID_VALUE -999; give syntax error when using?

I am trying to compare with specific constants in C, and I simplified my program as follows:

#include "stdio.h"
#include "stdlib.h"
#define INVALID_VALUE -999;

int main(void)
{
    int test=0;
    if(test==INVALID_VALUE) //The error line..
        return INVALID_VALUE;
    return 0;
}

And when I use gcc to compile, it throws an error " ". error: expected ‘)’ before ‘;’ token

Is there a reason this cannot be done?

+3
source share
6 answers

Remove the semicolon from the INVALID_VALUE definition.

Macros are replaced lexically (symbolically) without understanding the syntax around them. Your macro INVALID_VALUE is set -999;, so your if line extends the macro to:

if (test==-999;)

which is invalid C syntax

+10
source

; #define INVALID_VALUE -999;. . .

, , expected ‘)’ before ‘;’ token. , ) ;, , , ;. , INVALID_VALUE? #define INVALID_VALUE -999;, ! , , ? , . !

, #define , . , . :

, . . .

, .

, , . , .

+5

- C C


; .

. C , C.

16- 1970 , C , . , , , .

, C , . C, .

32- , , , , , , .

+3
source

Half Column at the End

#define INVALID_VALUE -999;

Classical.

+2
source

You do not need a semicolon after defining something. #define is actually a macro, and it will do an inline extension when compiling.

In this way,

#define IDENTIFIER 10;
int j = IDENTIFIER;

will expand as:

int j = 10;;
+2
source

Change macro

from

#define INVALID_VALUE -999;

to

#define INVALID_VALUE -999

Bye

+2
source

Source: https://habr.com/ru/post/1782204/


All Articles