Discussion:
#define and #undef influence over all the files (Multiple C Files)
(too old to reply)
karthikbalaguru
2008-11-27 12:42:03 UTC
Permalink
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
maverik
2008-11-27 13:20:04 UTC
Permalink
Post by 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
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 */
Post by karthikbalaguru
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)
pete
2008-11-27 13:42:53 UTC
Permalink
Post by 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
Either remove #define VERSION_1 from mydefines.h
or remove #include "mydefines.h" from dummy1.c.

Two other style things:
1 Header guards

/* BEGIN mydefines.h */

#ifndef H_MYDEFINES_H
#define H_MYDEFINES_H

void checkversionstatus(void);

#endif

/* BEGIN mydefines.h */

2 "include" before <include>

You should endeavor to write header files
so that their order of inclusion doesn't matter.
If you "include" your header files first,
then that will expose any deficiencies
which would have been hidden by <including>
the standard headers first.


#include "mydefines.h"
#include <stdio.h>
vs.
#include <stdio.h>
#include "mydefines.h"
--
pete
James Kuyper
2008-11-27 13:43:00 UTC
Permalink
karthikbalaguru wrote:
...
Post by karthikbalaguru
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.
Chris Dollin
2008-11-27 13:45:26 UTC
Permalink
Post by 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. :(
Of course. #undef affects /what follows it in the compilation unit/.
Post by karthikbalaguru
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.
Post by karthikbalaguru
How to make it appear undefined in another C file also ?
#undef it there, too.
Post by karthikbalaguru
Any tricks ?
If you don't want it defined, why not #undef it at the end of
your header file?
Post by karthikbalaguru
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.
--
"Who do you serve, and who do you trust?" /Crusade/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Loading...