mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-11 13:38:24 +00:00
29 lines
1.8 KiB
TeX
29 lines
1.8 KiB
TeX
\newpage
|
|
\subsection{The C preprocessor}
|
|
To have \texttt{gcc} stop compiliation after running through \texttt{cpp}, the \texttt{C preprocessor}, use \texttt{gcc -E <file name>}.
|
|
|
|
Imports in \lC\ are handled by the preprocessor, that for each \verb|#include <file1.h>|, the preprocessor simply copies the contents of the file recursively into one file.
|
|
|
|
Depending on if we use \verb|#include <file1.h>| or \verb|#include "file1.h"| the preprocessor will search for the file either in the system headers or in the project directory.
|
|
Be wary of including files twice, as the preprocessor will recursively include all files (i.e. it will include files from the files we included)
|
|
|
|
The \lC\ preprocessor gives us what are called \texttt{preprocessor macros}, which have the format \verb|#define NAME SUBSTITUTION|.
|
|
\rmvspace
|
|
|
|
\inputcodewithfilename{c}{code-examples/00_c/01_preprocessor/}{00_macros.c}
|
|
|
|
To avoid issues with semicolons at the end of preprocessor macros that wrap statements that cannot end in semicolons, we can use a concept called semicolon swallowing.
|
|
For that, we wrap the statements in a \texttt{do \dots\ while(0)} loop, which is removed by the compiler on compile, also taking with it the semicolon.
|
|
|
|
There are also a number of predefined macros:
|
|
\begin{itemize}[noitemsep]
|
|
\item \verb|__FILE__|: Filename of processed file
|
|
\item \verb|__LINE__|: Line number of this usage of macro
|
|
\item \verb|__DATE__|: Date of processing
|
|
\item \verb|__TIME__|: Time of processing
|
|
\item \verb|__STDC__|: Set if ANSI Standard \lC\ compiler is used
|
|
\item \verb|__STDC_VERSION__|: The version of Standard \lC\ being compiled
|
|
\item \dots many more
|
|
\end{itemize}
|
|
In headers, we typically use \verb|#ifndef __FILENAME_H_| followed by a \verb|#define __FILENAME_H_| or the like to check if the header was already included before
|