=================================== C By Example Part 2: C Preprocessor [TOC] =================================== In short: Using the option `-E` the `gcc` tool chain stops after running the C preprocessor (CPP). The result of CPP gets written to stdout (i.e. it gets written to the terminal). Use this to see what input the compiler actually gets. ---- VIDEO ------------------------------ https://www.youtube.com/embed/x9f-uqcm52E ----------------------------------------- Supplement: More About Preprocessor Directives ============================================== If you want to learn more about the preprocessor the internet is your friend. There are many web sites with great content, here are two of them: - __C Preprocessor (on gcc.gnu.org)__ is very comprehensive and perfect to look up each and every detail. - __C Preprocessor (on Wikipedia)__ gives a nice overview and summarizes all the other directives not mentioned in the video. Supplement: Compiler Options for Macro Definitions ================================================= Consider ---- CODE (file=session04/foo.c) --------------------------- hello world! ------------------------------------------------------------ With the option `-D` (__man gcc__) one can define macros. For example ---- SHELL (path=session04) -------------------------------- gcc -Dhello=Hallo -Dworld=Welt -E foo.c ------------------------------------------------------------ CPP uses this options as if the first two lines of input file would be ---- CODE (type=c) ----------------------------------------- #define hello Hallo #define world Welt ------------------------------------------------------------ You also can use the `-D` option to define an empty macro. Consider this source file with a conditional directive: ---- CODE (file=session04/bar.c) --------------------------- #ifdef de Hallo Welt! #else hello world! #endif ------------------------------------------------------------ with `-Dde` the macro is defined for the preprocessor processes the first line of the source file: ---- SHELL (path=session04) -------------------------------- gcc -E bar.c gcc -Dde -E bar.c ------------------------------------------------------------ Food for Thoughts ================= How could you use the `-D` option so that the token `dummy` gets expanded to the two tokens `foo bar`? Quiz 04 ======= Consider the following C program: ---- CODE (file=session04/obscure.c) ----------------------- What the(bloody hell); is this() { thing(doing); } ------------------------------------------------------------ Add some directives but otherwise do not change the content so that it can be compiled with ---- CODE ----------------------- gcc obscure.c --------------------------------- into an executable that prints ---- CODE ----------------------- hello, world! --------------------------------- Submit your solution with `submit hpc quiz04 obscure.c` *Note:* There are no automatic tests and hence you will get no confirmation email. However, if there is *no error message* then the submission was successful. And if there was an error message then not successful. ;-) :links: man gcc -> https://man7.org/linux/man-pages/man1/gcc.1.html C Preprocessor \(on gcc.gnu.org\) -> https://gcc.gnu.org/onlinedocs/cpp/ C Preprocessor \(on Wikipedia\) -> https://en.wikipedia.org/wiki/C_preprocessor