#define and #undef influence over all the files (Multiple C Files)

K

karthikbalaguru

Hi,
I have a Macro defined in the header file. This header file is being
used
in all the C files of my project . I undefined a Macro in one of the C
file and
tried to check if its status is undefined in another C file. But, it
appears to
be in defined status. :(

I understand that ' An identifier defined with #define is available
anywhere in
the source code until a #undef is reached.'. So, here in the
second file, though the header file has the #define, the #undef done
in my first file has not had any influence on the second file as there
is no real #undef in the second file after the #define.

How to make it appear undefined in another C file also ?
Any tricks ?

In short, how to make an #define'd value behave like a global variable
so that if it is undefined in one file, it is available as undefined
in
another file also ?

Below is a snapshot of dummy code for your reference -

mydefines.h
------------------
#define VERSION_1
void checkversionstatus(void);

dummy.c
--------------
#include <stdio.h>
#include "mydefines.h"
int main()
{
int i = 1;
if (1==i) {
#undef VERSION_1
}
#ifdef VERSION_1
printf(" VERSION_1 is defined \n");
#else
printf(" VERSION_1 is NOT defined \n");
#endif
checkversionstatus();
return 0;
}

dummy1.c
----------------
#include <stdio.h>
#include "mydefines.h"
void checkversionstatus(void) {
#ifdef VERSION_1
printf(" VERSION_1 is defined \n");
#else
printf(" VERSION_1 is NOT defined \n");
#endif
}

When i compile and execute the above code , i get the below output :-
VERSION_1 is NOT defined
VERSION_1 is defined

How to get an output as below ?
VERSION_1 is NOT defined
VERSION_1 is NOT defined

Thx in advans,
Karthik Balaguru
 
M

maverik

Hi,
I have a Macro defined in the header file. This header file is being
used
in all the C files of my project . I undefined a Macro in one of the C
file and
tried to check if its status is undefined in another C file. But, it
appears to
be in defined status. :(

I understand that ' An identifier defined with #define is available
anywhere in
the source code until a #undef is reached.'. So, here in the
second file, though the header file has the #define, the #undef done
in my first file has not had any influence on the second file as there
is no real #undef in the second file after the #define.

How to make it appear undefined in another C file also ?
Any tricks ?

In short, how to make an #define'd value behave like a global variable
so that if it is undefined in one file, it is available as undefined
in
another file also ?

Below is a snapshot of dummy code for your reference -

mydefines.h
------------------
#define VERSION_1
void checkversionstatus(void);

dummy.c
--------------
#include <stdio.h>
#include "mydefines.h"
int main()
{
int i = 1;
if (1==i) {
#undef VERSION_1
}
#ifdef VERSION_1
printf(" VERSION_1 is defined \n");
#else
printf(" VERSION_1 is NOT defined \n");
#endif
checkversionstatus();
return 0;

}

dummy1.c
----------------
#include <stdio.h>
#include "mydefines.h"
void checkversionstatus(void) {
#ifdef VERSION_1
printf(" VERSION_1 is defined \n");
#else
printf(" VERSION_1 is NOT defined \n");
#endif

}

When i compile and execute the above code , i get the below output :-
VERSION_1 is NOT defined
VERSION_1 is defined

How to get an output as below ?
VERSION_1 is NOT defined
VERSION_1 is NOT defined

Ok. When you define VERSION_1 in mydefines.h the scope of VERSION_1 is
mydefines.h (the scope of #define directive is translation unit
(6.10.3.5)). As you include mydefines.h to dummy.c and dymmy1.c the
preprocessor just copies #define VERSION_1 to this files. Then you
undefine VERSION_1 in one of the files. But in the second file it is
not undefined (the scope of #undefine directive is translation unit
(6.10.3.5)).

BTW, do you miss guards in mydefines.h?
Like

#ifndef MYDEFINES_H
#define MYDEFINES_H

....

#endif /* MYDEFINES_H */
if (1==i) {
#undef VERSION_1
}

I hope you understand that VERSION_1 will be undefined in any case (it
doesn't depend on i value)
 
J

James Kuyper

karthikbalaguru wrote:
....
In short, how to make an #define'd value behave like a global variable
so that if it is undefined in one file, it is available as undefined
in
another file also ?

You can't. That's what global variables are for.
 
C

Chris Dollin

karthikbalaguru said:
Hi,
I have a Macro defined in the header file. This header file is being
used
in all the C files of my project . I undefined a Macro in one of the C
file and
tried to check if its status is undefined in another C file. But, it
appears to
be in defined status. :(

Of course. #undef affects /what follows it in the compilation unit/.
I understand that ' An identifier defined with #define is available
anywhere in
the source code until a #undef is reached.'.

In /that compilation unit/ (a source file with all its includes). Not
in some other compilation unit.
How to make it appear undefined in another C file also ?

#undef it there, too.
Any tricks ?

If you don't want it defined, why not #undef it at the end of
your header file?
In short, how to make an #define'd value behave like a global variable
so that if it is undefined in one file, it is available as undefined
in
another file also ?

Why on Earth do you want to make life so fragile for yourself?

It seems to me that the easiest way to solve this problem is to
put your macro in its own header file and only #include it where
you want it.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top