Defining a variable as another variable

Jswale

Joined Jun 30, 2015
121
Hi

Can you define a variable as another variable?

e.g

C:
#define THIS        THAT

(in another file)....
THAT = 2;
Also, when using #define, was is the identifier?

JSwale

Papabravo

Joined Feb 24, 2006
13,712
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:
    THIS = 2 ;
THAT = 2 ;
will both result in the same code being generated assuming THAT is defined someplace at an "int'.

Jswale

Joined Jun 30, 2015
121
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
C:
int THAT;

WBahn

Joined Mar 31, 2012
25,747
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.

Papabravo

Joined Feb 24, 2006
13,712
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
C:
int THAT;
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.

dannyf

Joined Sep 13, 2015
2,197
Can you define a variable as another variable?
Yes. But your example has it backward: THIS is being defined as the same as THAT.

djsfantasi

Joined Apr 11, 2010
6,421
Not to confuse matters further, the pre processor will change ALL occurrences of THIS, even when it appears in different variables...
Code:
#define THIS THAT
THIS_THING=1;
THIS=2;
Becomes...
Code:
THAT_THING=1;
THAT=2;

Last edited:

dannyf

Joined Sep 13, 2015
2,197
Code:
#define THIS THAT;
Wow!

WBahn

Joined Mar 31, 2012
25,747
Not to confuse matters further, the pre processor will change ALL occurrences of THIS, even when it appears in different variables...
Code:
#define THIS THAT
THIS_THING=1;
THIS=2;
Becomes...
Code:
THAT_THING=1;
THAT=2;
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.

vpoko

Joined Jan 5, 2012
267
Not to confuse matters further, the pre processor will change ALL occurrences of THIS, even when it appears in different variables...
Code:
#define THIS THAT;
THIS_THING=1;
THIS=2;
Becomes...
Code:
THAT_THING=1;
THAT=2;
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.

WBahn

Joined Mar 31, 2012
25,747
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.

MrSoftware

Joined Oct 29, 2013
1,778
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.

Jswale

Joined Jun 30, 2015
121
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..

C:
#define Threshold          USER_CONFIG

later once executed....

if (USER_ENTERED == 5) USER_CONFIG = 10;
Therefore, will USER_CONFIG = 10 if they User enters 5 into the command terminal because the define has already been assigned.

MrSoftware

Joined Oct 29, 2013
1,778
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

}​

vpoko

Joined Jan 5, 2012
267
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..

C:
#define Threshold          USER_CONFIG

later once executed....

if (USER_ENTERED == 5) USER_CONFIG = 10;
Therefore, will USER_CONFIG = 10 if they User enters 5 into the command terminal because the define has already been assigned.
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.