#if command at header file

P

pristo

hi to all,
why when i use the command

#define test 10
#define X test;
#if (test == X) ....
......




The test is fail always when the statment at header file?
if this statment is found at c file the test work
how can i make it work(and still use defines)?


Pristo
 
G

Gordon Burditt

#define test 10
#define X test;
#if (test == X) ....
.....


The test is fail always when the statment at header file?
if this statment is found at c file the test work
how can i make it work(and still use defines)?

Manually expand it: does this line make any sense?

#if (test; == ) ...

From this, you should be able to tell which character to get rid of.

Gordon L. Burditt
 
W

Walter Roberson

why when i use the command
#define test 10
#define X test;
#if (test == X) ....
.....
The test is fail always when the statment at header file?
if this statment is found at c file the test work
how can i make it work(and still use defines)?

It should fail either way: the semicolon at the end of the X
definition is not valid preprocessor test syntax.

It fails either way for me for gcc 3.3 and SGI IRIX C 7.3.1.2m
 
K

Keith Thompson

pristo said:
why when i use the command

#define test 10
#define X test;
#if (test == X) ....
.....




The test is fail always when the statment at header file?
if this statment is found at c file the test work
how can i make it work(and still use defines)?

You haven't given us enough information to guess what the problem
might be.

Post your actual code (preferably reduced to a few lines) and show us
what the output is and how it differs from what you expected it to be.
 
K

Keith Thompson

Manually expand it: does this line make any sense?

#if (test; == ) ...

From this, you should be able to tell which character to get rid of.

But since he's seeing different behavior in a .h file vs. in a .c
file, I'm guessing that the code he posted bears only a superficial
resemblance to the code he's actually compiling. The semicolon
*might* be his problem, or it could be something else entirely.
 
P

pristo

Gordon said:
Manually expand it: does this line make any sense?

#if (test; == ) ...

From this, you should be able to tell which character to get rid of.

Gordon L. Burditt


it a type mistake i write a new smaple code

file1.h
#define PARAM1 10
#define PARAM2 20
#define PARAM3 30
end of file1.h


file2.h
#define TEST1 PARAM2
end of file2.h

file3.h
#if (TEST1 == PARAM2)
...
#else
...
#endif
end of file3.h


- if i put the same code of file3.h at c file the #if statment will
work fine else its not work
thanks for helping
Pristo
 
O

Old Wolf

pristo said:
file1.h
#define PARAM1 10
#define PARAM2 20
#define PARAM3 30

file2.h
#define TEST1 PARAM2

file3.h
#if (TEST1 == PARAM2)
...
#else
...
#endif
end of file3.h

- if i put the same code of file3.h at c file the #if statment will
work fine else its not work

In file3.h you have to write:
#include "file1.h"
#include "file2.h"

before anything else, and you have to either make sure that file1.h
and file2.h do NOT include file3.h, or use header guards (if you
don't know what header guards are, then do a google search).
 
K

Keith Thompson

pristo said:
it a type mistake i write a new smaple code

file1.h
#define PARAM1 10
#define PARAM2 20
#define PARAM3 30
end of file1.h


file2.h
#define TEST1 PARAM2
end of file2.h

file3.h
#if (TEST1 == PARAM2)
...
#else
...
#endif
end of file3.h


- if i put the same code of file3.h at c file the #if statment will
work fine else its not work
thanks for helping

You still need to show us *exactly* what you're trying to do. I
presume you don't literally have "..." lines in file3.h; what's really
there? You say it doesn't work; what exactly does that mean? What
does it do, and what did you expect it to do?

You're not showing us the actual code that you're compiling, just a
summary of it. The error is somewhere in what you're not showing us.
Don't try to guess what information you think we need.

Here's an example of the level of detail we need:

==================== file1.h ====================
#define PARAM1 10
#define PARAM2 20
#define PARAM3 30
=================================================

==================== file2.h ====================
#define TEST1 PARAM2
=================================================

==================== file3.h ====================
#if (TEST1 == PARAM2)
const char *message = "ok";
#else
const char *message = "error";
#endif
=================================================

==================== main1.c ====================
#include <stdio.h>
#include "file1.h"
#include "file2.h"
#include "file3.h"

int main(void)
{
printf("message = \"%s\"\n", message);
return 0;
}
=================================================

==================== main2.c ====================
#include <stdio.h>
#include "file1.h"
#include "file2.h"

#if (TEST1 == PARAM2)
const char *message = "ok";
#else
const char *message = "error";
#endif

int main(void)
{
printf("message = \"%s\"\n", message);
return 0;
}
=================================================

When I compile "main1.c" and execute the resulting program, the output is
message = "ok"
When I compile "main2.c" and execute the resulting program, the output is
message = "ok"
(identical to the output of main1).

This doesn't exhibit an error; if it did, I'd also specify what output
I expected to see.
 
K

Keith Thompson

Old Wolf said:
In file3.h you have to write:
#include "file1.h"
#include "file2.h"

before anything else, and you have to either make sure that file1.h
and file2.h do NOT include file3.h, or use header guards (if you
don't know what header guards are, then do a google search).

If the main program has

#include "file1.h"
#include "file2.h"
#include "file3.h"

you can get away with not having headers include other headers. It's
poor style, of course, since it places obscure constraints on what
your main program has to do. The best approach is for each file to
include just what it needs, and to use include guards as necessary to
avoid including a header more than once, but I don't think that's the
OP's problem. (But I'm only guessing, since we still haven't seen the
code he's actually compiling.)
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top