[Tex/LaTex] C code to add in the document

listings

I am new to LaTeX. I have to add C code in my document, I looked for examles and found the following way to add it. But the problem is there is no proper color displying like comments and code is displaying in the same color. Could you please tell me how to fix it?

Here is an example based on C code display in eclipse.

\begin{lstlisting}

int abc = 3; //Variable for reference

\end{lstlisting}

Best Answer

You can adopt this piece of code to customize more options

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\definecolor{mGreen}{rgb}{0,0.6,0}
\definecolor{mGray}{rgb}{0.5,0.5,0.5}
\definecolor{mPurple}{rgb}{0.58,0,0.82}
\definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{CStyle}{
    backgroundcolor=\color{backgroundColour},   
    commentstyle=\color{mGreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{mGray},
    stringstyle=\color{mPurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    language=C
}



\begin{document}
\begin{lstlisting}[style=CStyle]
#include <stdio.h>
int main(void)
{
   printf("Hello World!"); 
}
\end{lstlisting}
\end{document} 

The result is

enter image description here

Related Question