\usepackage{listings} \lstset{language=C} ... \begin{document} \begin{lstlisting} #include <stdio.h> int main(int argc, char ** argv) { printf("Hello world!\n"); return 0; } \end{lstlisting} \end{document}
or you can have it typeset whole files:
\usepackage{listings} \lstset{language=C} ... \begin{document} \lstinputlisting{main.c} \end{document}
These very simple examples may be decorated in a huge variety of ways, and of course there are other languages in the package’s vocabulary than just C...
Most people, advising others on (La)TeX lists, seem to regard listings as the be-all and end-all on this topic. But there are alternatives, which may be worth considering, in some situations. Highlight is attractive if you need more than one output format for your program: as well as (La)TeX output, highlight will produce (X)HTML, RTF and XSL-FO representations of your program listing. Documentation is available on the highlight project site. The lgrind system is another well-established pre-compiler, with all the facilities one might need and a wide repertoire of languages; it is derived from the very long-established tgrind, whose output is based on Plain TeX The tiny_c2l system is more recent: users are encouraged to generate their own driver files for languages it doesn’t already deal with. The C++2LaTeX system comes with strong recommendations for use with C and C++. An extremely simple system is c2latex, for which you write LaTeX source in your C program comments. The program then converts your program into a LaTeX document for processing. The program (implicitly) claims to be “self-documenting”.This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=codelist