Using macros in condition statements

L

Lep

Hi,

Whenever I try to use a macro in a condition statement such as:

#define VALID 1

and then do:

if(x == VALID)

my program won't compile and I get the following error.
error: expected ')' before ';' token|

But using the macro to assign a variable works fine such as
int x = VALID;

I found this only happens when its being called in a function outside
the main.c file.

I am using gcc.

Anyone else experience this?

Here's the complete program:

///////////////////////
// main.c
#include "test.h"
#include <stdio.h>

#define TRUE 1

int main()
{
test();

//TRUE statements compile fine, VALID error
int x = TRUE;
int y = VALID;

printf("%d\n", x);
printf("%d\n", y);

printf("%d\n", TRUE);
// printf("%d\n", VALID);

if(1 == TRUE) {}
// if(1 == VALID) {}

int a = 1;
switch(a) {
case TRUE:
break;
// case VALID:
// break;
}

return 0;
}

////////////////////
// test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

#define VALID 1;
#define INVALID 0;
#define INITIAL -1;

void test();

#endif // TEST_H_INCLUDED

/////////////////////

#include <stdio.h>
#include "test.h"

void test() {
int x = VALID;
int y = INVALID;
int z = INITIAL;

printf("%d\n", x);
printf("%d\n", y);
printf("%d\n", z);

// printf("%d\n", VALID);
// printf("%d\n", INVALID);
// printf("%d\n", INITIAL);

// if(1 == VALID) {}

// int a = 0;
// switch(a) {
// case INVALID:
// break;
// }

//Remove the comments from the lines above and compiling fails with
the following error:

// D:\Programming\C\macroTest\test.c||In function 'test':|
// D:\Programming\C\macroTest\test.c|13|error: expected ')' before ';'
token|
// D:\Programming\C\macroTest\test.c|14|error: expected ')' before ';'
token|
// D:\Programming\C\macroTest\test.c|15|error: expected ')' before ';'
token|
// D:\Programming\C\macroTest\test.c|17|error: expected ')' before ';'
token|
// D:\Programming\C\macroTest\test.c|21|error: expected ':' or '...'
before ';' token|
// D:\Programming\C\macroTest\test.c|21|error: expected expression
before ':' token|
// ||=== Build finished: 6 errors, 0 warnings ===|

}
 
J

James Lothian

Lep said:
Hi,

Whenever I try to use a macro in a condition statement such as:

#define VALID 1

and then do:

if(x == VALID)
[snip]
////////////////////
// test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

#define VALID 1;
#define INVALID 0;
#define INITIAL -1;

Macro definitions are not statements and don't get a semicolon on the end.

James
 
S

Seebs

Whenever I try to use a macro in a condition statement such as:

#define VALID 1

and then do:

if(x == VALID)

my program won't compile and I get the following error.
error: expected ')' before ';' token|

This is untrue.
#define VALID 1;

What you said above referred to "#define VALID 1". However, you have
written here "#define VALID 1;".

if (x == 1;)

think about it.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top