How to check if a variable exists

This is a discussion on How to check if a variable exists within the C Programming forums, part of the General Programming Boards category; Hi, I was hoping someone may be able to help me. What I want to do is basically perfrom an ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    How to check if a variable exists

    Hi,

    I was hoping someone may be able to help me. What I want to do is basically perfrom an if statement to check if a varaible has been declared. If it has, then perform some code, if not don't perform the code.

    Now I have tried my best to research this, but all I can find is #ifdef, which only seems to work if you declare a variable as #define.

    What I have got is:

    • file_a.h (int varname; )
    • file_b.c (I DO NOT #include file_a.h)



    I can't use #define because this only works if I am using the #ifdef in a file that includes the header file where the variable is declared #define, and in this case I am not.




    I want to say in file_b.c:

    if (varname exists)
    {

    Perform code;
    }


    Is this possible??? Any help would be much appreciated

    Thanks
    Chenaldo

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you need to check to see if a named variable exists? Give an example scenario if you will, because I'm not sure I see the point of this.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    Ok, sorry i didn't make that very clear.

    What I've got is several main files. Lets call them:

    file_1.h + file_1.c
    file_2.h + file_2.c
    file_3.h + file_3.c


    Each file is standalone, meaning no variables overalp into other files. However there is one generic file, lets call it (generic.c + generic.h) that references some variables from the other files externally,

    e.g. extern int varname;


    There are occasions where a file e.g. file_1.c and file_1.h are not included in the overall program i.e. missing when the code is run.

    So I need to have a way of checking to see if variables being referenced externally in generic.h have actaully been declared in file_1.h. If they haven't i.e file_1.h is missing this will not effect generic.h or generic.c because the variables will not be referenced.


    Example:

    file_1.h:

    Code:
    int varname;


    generic.h:
    Code:
    if (varname exists)
    {
       extern int varname;
    }
    
    int newvariable;
    generic.c:

    Code:
    if(varname exists)
    {
    
       newvariable = varname;
    }
    
       other code........


    I hope this example makes more sense,

    thanks for your help its much appreciated

    Chenaldo

  4. #4
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    It's not possible to query if a symbol exists or not. (Ok, it is "possible", but not what you want). It doesn't make too much sense for compiled code.

    I don't quite understand what you are trying to do either. Your program won't even link if an external does not exists in any of the linked objects. In consequence, you cannot use any runtime code to check for existance. You *will* have to do this with preprocessor commands (which are processed before compilation)
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Well in file_1.h you probably have some something to prevent multiple exclusion like:

    Code:
    #ifndef FILE_1_H
    #define FILE_1_H
    
    // file_1.h contents
    
    #endif
    Then in generic.c you can do:

    Code:
    #ifdef FILE_1_H
        newvariable = varname;
    #endif
    Another (better) option would be to compile file_1.h and file_1.c into a library, and then link to that library dynamically from generic.c

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    9
    Is there any good reason to not include file[1, 2, 3].h in generic.h?
    The only way of knowing if a variable is defined externally is to use it - if it's not in the .o/.lib file you link to later, the linker will bail out, if it is there it will link.
    This is the kind of stuff header files are used for, maybe use (yet) another header file, where the external var's are declared?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    See the code attached
    It uses a little bit of magic called tentative definitions

    I get these results
    Code:
    $ gcc generic.c && ./a.out
    main starts
    main ends
    $ gcc generic.c file1.c && ./a.out
    main starts
    Var1=42
    main ends
    $ gcc generic.c file2.c && ./a.out
    main starts
    Var2=24
    main ends
    $ gcc generic.c file1.c file2.c && ./a.out
    main starts
    Var1=42
    Var2=24
    main ends
    You might want to look at using the -DVAR_EXISTS type of construct on the command line (which creates #define VAR_EXISTS) so you can control what happens from the command line without having to edit files.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  3. Replies: 14
    Last Post: 11-17-2008, 12:31 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21