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 ===|
}
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 ===|
}