Defining a variable as another variable

Discussion in 'Programmer's Corner' started by Jswale, Oct 1, 2015.

  1. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    Hi

    Can you define a variable as another variable?

    e.g

    Code (C):
    1. #define THIS        THAT
    2.  
    3. (in another file)....
    4. THAT = 2;
    Also, when using #define, was is the identifier?

    JSwale
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    The purpose of #define is to perform "text substitution" before the compiler runs it's first pass on the input text. In your example, the preprocessor will look for all occurrences of the string "THIS" and substitute the string "THAT". It has no knowledge of what you are using those strings for. It has no ideas about variables or keywords or anything else. So:
    Code (Text):
    1.  
    2.     THIS = 2 ;
    3.     THAT = 2 ;
    4.  
    will both result in the same code being generated assuming THAT is defined someplace at an "int'.
     
  3. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    But if I change what THAT is later in the program, will the define recognise this? because the #define will be executed before the main program.

    So, if I were to get THAT value through a keypad from a User...or would it be equal to the initial definition of
    Code (C):
    1. int THAT;
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,792
    Your #define replaces all occurrence of the token "THIS" with the text "THAT". It does not do anything with any occurrences of "THAT" anywhere.

    The key to understanding what will happen is to imagine that you use a text editor to perform a global search and replace to get rid of "THIS" and replace it with "THAT" anywhere in your program, subject to the constraint that the replacement only happens if "THIS" forms a token.
     
    vpoko likes this.
  5. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    You still don't get what is going on. Reread my original answer and focus on what job is performed by the "C Compiler Preprocessor" and what job is performed byt eh the "C Compiler Pass #1". I'll give you a hint. Pass #1 performs lexical analysis and builds a symbol table of identifiers.

    After the "C preprocessor" runs all of the instances of "THIS" in the source code will be replaced with "THAT". After compilation is finished and the executable is running there are no occurences ot THIS and the preprocessor has no further ability to affect ANYTHING! Period, full stop.
     
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    1,792
    360
    Yes. But your example has it backward: THIS is being defined as the same as THAT.
     
  7. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,804
    832
    Not to confuse matters further, the pre processor will change ALL occurrences of THIS, even when it appears in different variables...
    Code (Text):
    1. #define THIS THAT
    2. THIS_THING=1;
    3. THIS=2;
    Becomes...
    Code (Text):
    1. THAT_THING=1;
    2. THAT=2;
     
    Last edited: Oct 2, 2015
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,792
    360
    Code (Text):
    1. #define THIS THAT;
    Wow!
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,792
    No it won't. The preprocessor only replaces tokens that are equal to THIS with THAT. THIS_THING is a single token. The preprocessor also won't change occurrences of THIS that appear in string literals or in comments.
     
  10. vpoko

    Member

    Jan 5, 2012
    258
    47
    WBahn beat me to it, but this is not correct. It's not a find/replace, the preprocessor tokenizes your source code and operates on tokens, not on text. One (very minor) clarification since WBahn mentioned comments: the preprocessor actually deletes all comments entirely, which makes sense since the compiler doesn't care about them.
     
  11. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,792
    Yep. While most descriptions of #define stipulate that preprocessing tokens within comments are not expanded, the actual compiler semantics require that comments be replaced by a single space character during Phase 3 while preprocessor macro expansion takes place in Phase 4.
     
  12. MrSoftware

    Active Member

    Oct 29, 2013
    500
    122
    Spend a few minutes and read up on the C/C++ compilation process, that will answer your questions. There are tons of articles, here's the first search result I got and it will help you:

    http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html

    In short, #define does not give you a variable. When you compile C/C++, the first step is the preprocessor parses the code and values defined with #define are essentially search/replaced with the defined values. For example, if your c file has this:

    #define ONE 1

    main()
    {
    int i = ONE;
    }​

    Before the code is compiled, the preprocessor will parse the code and change it to this:

    main()
    {
    int i = 1;
    }​

    The value ONE is completely gone, removed by the preprocessor. So if you try this:

    #define ONE 1
    #define TWO 2

    main()
    {
    int i = ONE;
    TWO = ONE;
    }
    The preprocessor will change this to:

    main()
    {
    int i = 1;
    2 = 1;
    }​

    This is obviously an error and won't compile. You can give some compilers an option to output the code after the preprocess step, so you can actually see what has happened. The preprocessor is extremely powerful and can do a ton of stuff, but this is one of the more basic and more frequently used features.

    If you're just starting out in C/C++ and you want to make your variable visible in other c files, you can use a global. Google accessing globals from other c files. Hint; you'll need to use the extern key word. Your global will be allocated in one place, most likely your main cpp file, then referenced as extern from your other files. The extern key word tells the linker that the memory for the variable was allocated elsewhere, and the linker will go look for the memory allocation in your main object file. Read up on the steps of C/C++ compilation and it will all make sense. Understanding the compilation step and the link step are key.

    As you get more proficient, stay away from globals, but at the beginning it might get you through. I hope this helps.
     
  13. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    Yes, I get that. I just have a unusual situation...

    I am defining something that does not get assigned a variable until the program is executed and the User inputs (via command terminal) the value.

    A quick example of the problem..

    Code (C):
    1. #define Threshold          USER_CONFIG
    2.  
    3.  
    4. later once executed....
    5.  
    6. if (USER_ENTERED == 5) USER_CONFIG = 10;
    7.  
    8.  
    Therefore, will USER_CONFIG = 10 if they User enters 5 into the command terminal because the define has already been assigned.
     
  14. MrSoftware

    Active Member

    Oct 29, 2013
    500
    122
    I'm not clear exactly what you're trying to do, so I don't know if this is a solution.. but this will make your code compile:

    #define Threshold USER_CONFIG
    int USER_CONFIG; // global var, make this local if possible
    main()
    {
    //.. blah blah..
    if(USER_ENTERED == 5) USER_CONFIG=10; // This is OK now, USER_CONFIG is a variable

    // This is OK now too, "Threshold" will be replaced with "USER_CONFIG" by the preprocessor
    Threshold = 12345;

    // Now USER_CONFIG = 12345

    }​
     
  15. vpoko

    Member

    Jan 5, 2012
    258
    47
    Yes, if later in your code (after the assignment) you did something like printf("%i", Threshold), the preprocessor would change it to printf("%i", USER_CONFIG). It would do the same thing before the assignment, too (or if the assignment is skipped completely because the user entered something other than 5) but then you'd be trying to use an unassigned variable.
     
Loading...