If a macro is not getting expanded as per your expectations how will you find out how is it being expanded by the preprocessor. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

If a macro is not getting expanded as per your expectations how will you find out how is it being expanded by the preprocessor.

14th Dec 2018, 6:27 AM
AKS
AKS - avatar
2 Answers
+ 7
Very interesting! "how will you find out how is it being expanded" You can see the code after the preprocessor gets substituted by adding `-E` flag to the command line parameters in order to make sure that everything went well. Example: #define X 2 * 20 #define Y(n) 2 * n int main() { int a = X; int b = Y(5); } Command line options: g++ -E main.cpp && ./a.out Output: # 1 "main.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "main.cpp" int main() { int a = 2 * 20; int b = 2 * 5; } _____ Live version: https://coliru.stacked-crooked.com/a/9c6c42e24e1cc3ad
14th Dec 2018, 7:52 AM
Babak
Babak - avatar
+ 1
In Linux using the gcc compiler there is an option to keep the source created by the preprocessing step and then either aborting or continuing the compile. You can examine the output of the preprocessor to see how the macro is being expanded. -E expands macros then quits -save-temps saves all temp files created during compile. The processed C or C++ file will then be available for examination.
6th Feb 2019, 10:31 PM
Roger Greenlaw