I can not write else if sentence in C programs

Hi all,

I have encountered an problem that doubt me a lot, the problem is what you see below:

I have write a little program as what you see below:
---------------------------------------------------------------
#include<stdio.h>
#define IN 1;
#define OUT 0;
int main() {
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;

while ((c = getchar()) != EOF) {
++nc;
if (c == '\n') {
++nl;
}

if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
}else if (state==OUT) { state=IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
-------------------------------------------------------------------


then i received the message after I complied the program as what you see below:
-----------------------------------------------------------------------
D:\workspace\a>gcc sample_001.c
sample_001.c: In function `main':
sample_001.c:27: error: syntax error before ';' token
sample_001.c: At top level:
sample_001.c:33: error: syntax error before string constant
sample_001.c:33: error: conflicting types for 'printf'
sample_001.c:33: note: a parameter list with an ellipsis can't match an empty pa
rameter name list declaration
sample_001.c:33: error: conflicting types for 'printf'
sample_001.c:33: note: a parameter list with an ellipsis can't match an empty pa
rameter name list declaration
sample_001.c:33: warning: data definition has no type or storage class
D:\workspace\a>gcc sample_001.c
sample_001.c: In function `main':
sample_001.c:27: error: syntax error before ';' token
sample_001.c: At top level:
sample_001.c:32: error: syntax error before string constant
sample_001.c:32: error: conflicting types for 'printf'
sample_001.c:32: note: a parameter list with an ellipsis can't match an empty pa
rameter name list declaration
sample_001.c:32: error: conflicting types for 'printf'
sample_001.c:32: note: a parameter list with an ellipsis can't match an empty pa
rameter name list declaration
sample_001.c:32: warning: data definition has no type or storage class
-----------------------------------------------------------------------

but when I modified the else if sentence "else if (state==OUT) " to "else if (state==0) ", means I changed the OUT with an integer value '0'(zero) the program seems goes well.

Can anyone tell me,whats wrong with my programs ?

Thanks
Last edited on
In the
[code]
1
2
3
#include<stdio.h>
#define IN 1;
#define OUT 0; 
[/code]

you have semicolons (;) in the macro definition, which is causing your conditional statement to look like this
 
else if (state==0;)


Be careful how you use macros. In this case, get rid of the semicolons:
1
2
3
#include<stdio.h>
#define IN 1
#define OUT 0 


For more safety, I would also bracket the values with parentheses:
1
2
3
#include<stdio.h>
#define IN (1)
#define OUT (0) 


Hope this helps.
Last edited on
hi Duoas, you are right, and now the programs goes well, Thank U very much.
Topic archived. No new replies allowed.