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

Discussion in 'C Programming' started by karthikbalaguru, Nov 27, 2008.

  1. 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
     
    karthikbalaguru, Nov 27, 2008
    #1
    1. Advertising

  2. karthikbalaguru

    maverik Guest

    Re: #define and #undef influence over all the files (Multiple CFiles)

    On Nov 27, 3:42 pm, karthikbalaguru <>
    wrote:
    > 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)
     
    maverik, Nov 27, 2008
    #2
    1. Advertising

  3. karthikbalaguru

    James Kuyper Guest

    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.
     
    James Kuyper, Nov 27, 2008
    #3
  4. karthikbalaguru

    Chris Dollin Guest

    karthikbalaguru wrote:

    > 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.

    --
    "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
     
    Chris Dollin, Nov 27, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Callea
    Replies:
    4
    Views:
    509
    David Browne
    Nov 9, 2004
  2. =?Utf-8?B?V2hpc2t5Um9tZW8=?=

    How to influence who the User is

    =?Utf-8?B?V2hpc2t5Um9tZW8=?=, Feb 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    Alec MacLean
    Feb 13, 2006
  3. Peter Hartmann
    Replies:
    1
    Views:
    305
    Tim Roberts
    Sep 18, 2005
  4. myheartinamerica

    saving a #define before an #undef

    myheartinamerica, Mar 5, 2008, in forum: C Programming
    Replies:
    2
    Views:
    339
    myheartinamerica
    Mar 5, 2008
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    162
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page