0

Is there a way to see what the expanded code will look like after preprocessing? Found many answers to my question online but all of them used a lot of terms which were completely alien to me. Seeing expanded C macros Determine the expansion of a C macro I am a complete beginner in programming. Learning C from 'Let us C' by Y.K. Currently in the 12th chapter 'C Preprocessor'. Can someone explain this in simple terms or should I just leave it for now and get back to it later?

using Code:Blocks as the IDE and gcc as the compiler.

Edit:-

#include<stdio.h>
#define AREA(x) (3.14*x*x)
int main()
{
    int r1=1,r2=2;
    float ar1,ar2;
    ar1=AREA (r1);
    printf(" %f",ar1);
    ar2=AREA (r2);
    printf(" %f",ar2);
    printf(" %f",ar1/ar2);
    printf(" %f",AREA (r1)/AREA (r2));
}
EMS
  • 104
  • 1
  • 6

2 Answers2

3

Compile your code from the terminal, with the -E flag: gcc my_code.c -E.

This will print the preprocessed code directly to the terminal. If you want to save it to a file instead, add something like -o result.txt at the end.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • By terminal, you mean cmd? – EMS Mar 28 '21 at 17:34
  • @EMS Yep, exactly. – HolyBlackCat Mar 28 '21 at 17:41
  • ('gcc' is not recognized as an internal or external command, operable program or batch file.) getting this error. – EMS Mar 28 '21 at 17:45
  • @EMS Find where `gcc.exe` is on your system. Then add that directory to PATH as described [here](https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/). Then it should work. Make sure you close and restart CMD after modifying PATH. – HolyBlackCat Mar 28 '21 at 18:50
-1

Read the documentation of the GCC compiler.

Read also some C reference website.

There is a chapter about Invoking GCC. Spend an hour to read it.

You also want to read the documentation of the CPP preprocessor, and probably of GNU binutils.

You could compile your C file ems.c using gcc -Wall -Wextra -g -H ems.c -o ems-prog. Once you have no warnings, use the GDB debugger to understand the behavior of your executable ems-prog

You could run gcc -Wall -Wextra -g -C -E ems.c -o ems.i to obtain, inside the ems.i file, the preprocessed form of ems.c

You can run compilation commands in some terminal emulator.

Later, you want to use GNU make (or ninja) or some other build automation tool to drive compilation commands. Of course, you need to read their documentation.

Study for inspiration the source code of existing free software coded in C, for example GNU bash.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 2
    You might want to explicitly say that the only truly necessary flag is `-E`, so the minimal command is something like `gcc -E ems.c -o ems.i`. – HolyBlackCat Mar 28 '21 at 16:27
  • In general yes, but some optimization flags may add preprocessor flags. And keeping the comments intact is practically useful – Basile Starynkevitch Mar 28 '21 at 16:28
  • 2
    Then you could say that OP should use the same flags they use normally, **plus** `-E`. – HolyBlackCat Mar 28 '21 at 16:29
  • 3
    I wanted to ask for a while, what's the idea behind including links to tangentally related topics in your answers? You think SO would be a better place if every answer was like this? I always thought that SO got so popular because it's a no-nonsense Q&A. If I were searching for "how to expand macros", I'd want to see something like *"compile your code with `-E` flag, like this: , see more in the "*, not dig through half-dozen links to build systems, shells, debuggers, and what not. :/ – HolyBlackCat Mar 28 '21 at 16:35
  • The major idea is to read documentation before asking. I don't understand why people prefer to ask questions on SO before reading doicumentation, and without providing any [mre] – Basile Starynkevitch Mar 28 '21 at 16:38
  • Okay, this justifies the link to GCC documentation. But what about GDB, Make, Ninja, Bash? They seem to be barely related, if at all. Regarding MCVE, I don't feel this question warrants one. As your answer shows, you don't need a MCVE to answer it. – HolyBlackCat Mar 28 '21 at 16:42
  • GDB is needed to OP to understand what his/her program does. Other are examples of existing software. – Basile Starynkevitch Mar 28 '21 at 16:48
  • I agree that those could be useful *in theory*, but imagine if every answer was like this. All the extra information would be overwhelming, don't you think? – HolyBlackCat Mar 28 '21 at 17:06
  • If I were you and wanted to share the extra information, I'd probably write a short article with all the useful links for those learning C, host it somewhere on the net, and link it at the end of my answers. This way you can share all the extra info you want, and keep SO answers clean. – HolyBlackCat Mar 28 '21 at 17:12
  • People don't read documentation, and don't read the many already written articles. And it has been years since SO answers are not clean. – Basile Starynkevitch Mar 28 '21 at 18:08
  • @BasileStarynkevitch By the way, I did come across many articles and documentation while searching for answers online, but they were too complicated and had a lot of new terminology for a complete beginner in programming like me(which I mentioned in my explanation of the question). I read them but couldn't make any sense of them. – EMS Mar 29 '21 at 03:38
  • @BasileStarynkevitch Thank you for the documentation anyway. It is useful in other ways. – EMS Mar 29 '21 at 03:41