Reputation: 3
if I define a macro like below:
#define TEST_VARIABLE 10
How does the compiler store it internally? as an signed/unsigned integer?
I have a loop in my program:
for (unsigned int loop = 0; loop < TEST_VARIABLE; loop++)
I want to check if extra instruction is added by compiler to type cast "loop" variable while comparing with TEST_VARIABLE. if TEST_VARIABLE is stored in different data type, extra instruction shall be required.
Upvotes: 0
Views: 274
Reputation: 225637
Macros created by #define
are basically text substitution, and are handled by the preprocesser. The result of the preprocessor is then handed to the compiler.
So when the preprocessor finishes with your code, the result is:
for (unsigned int loop = 0; loop < 10; loop++)
Then the compiler reads and compiles this code. So in this particular case, you have a numeric constant. The type of this constant is int
, since there is no type suffix or cast.
Upvotes: 1
Reputation: 782624
When the preprocessor performs macro replacement, it treats it as text. The fact that the replacement looks like a number is totally irrelevant during macro processing. When the compiler processes the result, it's exactly as if you'd typed the replacement in its place. So
for (unsigned int loop = 0; loop < TEST_VARIABLE; loop++)
is treated identically to
for (unsigned int loop = 0; loop < 10; loop++)
The compiler will interpret 10
as a signed int
.
Upvotes: 3