#define help please

This is a discussion on #define help please within the C Programming forums, part of the General Programming Boards category; Hello, can someone please explain to me exactly how #define works? I need to be able to flip the value ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    #define help please

    Hello,

    can someone please explain to me exactly how #define works? I need to be able to flip the value of a #define variable, between true and false, within a method call. My attempts so far have been fruitless - it appears that when you type #define inline the program begins with the lowest down #define in the code. EG if I have:

    Code:
    #define TRUE 1;
    #define FALSE 0;
    
    #define variable TRUE;
    
    if(condition) {
    #undef variable;
    #define variable FALSE;
    }
    My program will always begin with variable set to false.

    Explanations appreciated!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,665
    #define is not used to define variables. #define is a simple command to the preprocessor to replace all the text it sees with the given value you provided.
    In your example, it will replace every TRUE it finds with "1;".
    Perhaps you should learn more about #defines, the preprocessor and variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Well, I haven't come to this forum as a first resort. I just want to know if it is possible to "re-point" a #define ?substitution rule? So, in my example, the program replaces all text "variable" with "TRUE" which is in turn replaced by 1. Is it possible to "re-point" variable at any point in the program? Or am I being a newb and going about this all the wrong way :s

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,665
    It's possible, but not recommended. Whenever you do a a #define, the preprocessor will start replacing all the text from that point onwards. So you can do a "#define variable TRUE" somewhere and it will replace all the "variable" with "TRUE".
    You can also undefine a define and then define it again, so you redefine it to something else.
    But do not two things first:

    - The preprocessor runs only once. So if you replace something with TRUE, then it will be TRUE in the code and not 1.
    - Defines should not end with a ";". Everything after the space of the define is what the preprocessor will replace the text with.

    So "#define TRUE 1;" will replace every "TRUE" with "1;" and not "1".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    But surely then, as in my example, only the code following the #define will benefit from the change in the value?

    What I want to be able to do is change a "global variable" during the running of the program; a "global variable" that a number of conditions are based upon. I don't think that a #define rule is going to do the trick here.

    I really appreciate your help, you have confirmed my suspicions, but I don't know where to go from here.

    I really want to type private boolean debugModeOn; :s

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    That's not a global variable in your code.

    a global variable doesn't start with #define

    If you have a global variable say:

    int x = 10;

    Then anywhere in the program you can change that value and it will be the stored and not lost. Also I'm interested to know what 'conditions' that you are talking about.
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Well I've certainly learned that now. The implicit question was; "what is a global variable in C?".

    EDIT:

    Ah, ok, I see now. At any point during the running of my program debug mode can be turned on or off with user input. (That's the goal anyway!). I'll go and try this out.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,665
    A global variable is just a variable that is defined at global scope, ie outside any functions.
    Code:
    int myint;
    int main()
    myint is global.
    But global variables should be avoided, if possible.

    As to your original question, you suspicion is correct. Define is not suited for global variables. It is suited for constants and macros.
    Last edited by Elysia; 04-16-2008 at 02:49 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    A global variable is a variable you declare outside a function typically before the main function. Unlike variables declared inside functions where the values are lost when the function exits, the global variable retains it's value between function calls.

    So if you have defined x = 10 as an int globally. It will remain that way until changed, if changed, it will keep that change until the program exits.
    =========================================
    Everytime you segfault, you murder some part of the world

  10. #10
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Elysia View Post
    But global variables should be avoided, if possible.
    Exactly. It's better to put use structs.

    Eitherways, for small programs it doesn't matter about global variables and can be the most efficient way to implement a solution.
    =========================================
    Everytime you segfault, you murder some part of the world

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,665
    This may or may not be, but one should consider avoiding them since it's bad practice and will also make avoiding the decision of when the program is big enough to do away with them.
    I don't recommend using them at all, unless necessary, not even in small programs. But your mileage may vary. Just take this into account.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note also that the preprocessor does not do any sort of interpretation of just about anything that isn't starting with # or has a "name" in it that is a #define. So for example, we can write this (horrible thing):
    Code:
    #define A 1
    
    int main()
    {
        for(i = A;  // 1
    #undef A
    #define A 11
              i < A; i++)    // 2
        {
    #undef A
    #define A 100
              printf("i=%d A = %d\n", i, A);    // 3
        
        }
        return 0;
    }
    1. A is replaced with 1
    2. A is replaced by 11
    3. A is replaced by 100

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    I think I've found a solution;

    Code:
    int myLittleInt = 0;
    
    #define debugOn myLittleInt
    #define DEBUG debugOn
    
    .....
    
    switch(command)
    
    case DEBUG: flipDebug(); break;
    
    .....
    
    void
    flipDebug(void) {
    if(1 == myLittleInt)
         myLittleInt = 0;
    else
         myLittleInt = 1;
    }
    
    
    
    /*I know that I could probably just have #define DEBUG myLittleInt, but I want it like this for reasons that will be unapparent in this cut down example*/
    Thanks for your help folks!

  14. #14
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    That's not going to work
    =========================================
    Everytime you segfault, you murder some part of the world

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,665
    This is not really a good idea (not to mention you can't switch in a non-constant expression). It's confusing to say the least. Instead, it's better to do something such as:
    Code:
    typedef int bool;
    #define false 0
    #define true 1
    bool debug = true;
    
    ...
    if (debug)
    ...
    
    void flipDebug(void)
    {
        debug = !debug;
    }
    Last edited by Elysia; 04-16-2008 at 03:34 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM

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