# How to Format C Program Code Block Nicely

I am using the following settings to format my code blocks:

% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet STARTS xxxxxxxxxxxxxxxxxxxxxx
\lstset{
language=C,                     % choose the language of the code
stepnumber=1,                   % the step between two line-numbers. If it's 1, each line will be numbered
% numbersep=5pt,                 % how far the line-numbers are from the code
%  backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
tabsize=4,                      % sets default tabsize to 2 spaces
captionpos=t,                   % sets the caption-position to top
breaklines=true,                % sets automatic line breaking
breakatwhitespace=true,         % sets if automatic breaks should only happen at whitespace
% title=\lstname,                % show the filename of files included with \lstinputlisting;
identifierstyle=\color{identifierColor},
caption={Array of Pointers to Strings},
frame=lrtb,
keywordstyle=\color{purple},         % keyword style
commentstyle=\color{blue},           % comment style
stringstyle=\color{violet},          % string literal style
belowcaptionskip = 0.2in,            % Space below caption
abovecaptionskip = 0.2in,            % Space above caption
}
\begin{lstlisting}

int main()
{
char *strings[] = {
"String1",
"String2",
"String3",
"String4"
};

char *ptr_swap;   /* A temporary pointer to swap strings */

/* Swap "String2" with 'String3' */
ptr_swap  = names [1];
names [1] = names [2];
names [2] = ptr_swap;

return 0;
}
% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet ENDS xxxxxxxxxxxxxxxxxxxxxx


The Output is as follows:

My Question is:

The C Keywords (like: int, char) are NOT differently colored than the rest of the code.

I want to obtain a more beautiful output (like in professional books).

Please guide me on this issue.

Thanks.

-
int, char and return have indeed a different shade on my monitor. They are red-purple-ish, while variable names are red-orange-ish. Do you have a slight form of daltonism maybe? (no, I am not trolling, I am serious). –  Federico Poloni Aug 16 '12 at 9:46
What I remember from my Pascal days: identifierstyle=\color{black}, keywordstyle={\color{blue}\bf}, commentstyle=\color{gray}, stringstyle=\color{green!40!gray}. –  Tom Bombadil Aug 16 '12 at 11:29
@Federico: I was looking for a gvim kind of output. –  Sandeep Singh Aug 16 '12 at 12:47

Well, I'm not sure I really understood your question, as on the output you show, the keywords seem very much to be coloured, but in case you want to add coloured keywords, the following options seems to do the trick :

morekeywords={*,keyword1, keyword2, ...}


In case you want your keywords not to be coloured, then just set keywordstyle=\color{purple}, to the same colour as your stringstyle option.

As pointed out by @FedericoPoloni , purple and violet are also indeed very close colours. Maybe you would like to make sure by using less close ones.

In addition to that I'd also like to point out that in your example, the \lstset command has a comma after the last option, which can cause you a lot of problems.

And I know that it doesn't belong to your question, but the C code you are typing out seems to be very wrong. But it also could be that we lack a lot of context and it's not that important anyway.

-
Thanks for pointing out the mistake in lstset. The example code was vaguely adapted from somewhere to highlight the formatting issue only :) –  Sandeep Singh Aug 16 '12 at 12:46