Show how macros may cause bugs [closes #17]

This commit is contained in:
Antti H S Laaksonen 2017-04-23 10:29:01 +03:00
parent 335b28a438
commit a4d7df8d1e
1 changed files with 30 additions and 0 deletions

View File

@ -508,6 +508,36 @@ REP(i,1,n) {
} }
\end{lstlisting} \end{lstlisting}
Sometimes macros cause bugs that may be difficult
to detect. For example, consider the following macro
that calculates the square of a number:
\begin{lstlisting}
#define SQ(a) a*a
\end{lstlisting}
This macro \emph{does not} always work as expected.
For example, the code
\begin{lstlisting}
cout << SQ(3+3) << "\n";
\end{lstlisting}
corresponds to the code
\begin{lstlisting}
cout << 3+3*3+3 << "\n"; // 15
\end{lstlisting}
A better version of the macro is as follows:
\begin{lstlisting}
#define SQ(a) (a)*(a)
\end{lstlisting}
Now the code
\begin{lstlisting}
cout << SQ(3+3) << "\n";
\end{lstlisting}
corresponds to the code
\begin{lstlisting}
cout << (3+3)*(3+3) << "\n"; // 36
\end{lstlisting}
\section{Mathematics} \section{Mathematics}
Mathematics plays an important role in competitive Mathematics plays an important role in competitive