Writing a backslash in the \texttt environment

You can use \textbackslash rather than $\backslash$ to keep the current font. With $..$ you change to math font.

\texttt{This is a backslash: \textbackslash.}

How about using \verb instead of \texttt?

The first exercise uses \verb$printf("Hello world!\n");$.

You can use (almost) any character you like instead of $.

Better still, if you're typesetting a lot of code, use the listings package that provides a \lstinline macro, equivalent to \verb but with syntax highlighting.


or you can try: \texttt{\char`\\} (for me it looks better than \texttt{\textbackslash})