How to align left and right on the same lines

Here is a suggestion using listings.

\documentclass{article}
\usepackage{listings}
\lstset{
  language=C,
  frame=tb,
  escapeinside={@:}{:@},
  showstringspaces=false,
  basicstyle=\ttfamily
}

\newcommand\Comment{\hfill\normalfont\itshape}
\begin{document}

\begin{lstlisting}[caption={First C code}]
#include <stdio.h> @:\Comment include information about standard library:@

main() @:\Comment define a function named \texttt{main} :@
@:\Comment that receives no argument values :@
{
    printf("hello world\n");
}

\end{lstlisting}
\end{document}

enter image description here


A variant of Torbjørn's answer, where we can decide the comment box has no height (but some manual adjustment may become necessary).

\documentclass{article}
\usepackage{xparse}
\usepackage{listings}
\lstset{
  language=C,
  frame=tb,
  escapeinside={@:}{:@},
  showstringspaces=false,
  basicstyle=\linespread{1.4}\ttfamily,
  columns=fullflexible,
}

\NewDocumentCommand{\Comment}{O{\depth}m}{%
  \hfill\normalfont\itshape
  \renewcommand{\arraystretch}{0.7}%
  \raisebox{0pt}[\height][#1]{%
    \begin{tabular}[t]{@{}r@{}}
    #2
    \end{tabular}%
  }%
}
\DeclareTextFontCommand{\textttup}{\upshape\ttfamily}
\newcommand{\escape}[1]{\textttup{\symbol{`\\}#1}}

\begin{document}

\begin{lstlisting}[caption={First C code}]
#include <stdio.h>     @:\Comment{include information about standard library}:@
main()                 @:\Comment{define a function named \textttup{main}\\
                                  that receives no argument values}:@
{                      @:\Comment{statements of \textttup{main} are enclosed in braces}:@
    printf("hello world\n"); @:\Comment[0pt]{\textttup{main} calls library function \textttup{printf}\\
                                             to print the sequence of characters;\\
                                             \escape{n} represents the newline character}:@
}
@:\vspace{-.5\baselineskip}:@
\end{lstlisting}
\end{document}

enter image description here


Below I've set the code inside a figure (to allow it to float, if needed), while the structure is defined inside a tabular.

enter image description here

\documentclass{article}

% Insert a code comment: \codecomment{<comment>}
% Comment is set in a [t]op-aligned tabular that has tight
% vertical spacing and also overlaps with content below it.
\newcommand{\codecomment}[1]{{%
  \renewcommand{\arraystretch}{0}%
  \normalfont\itshape\hfill
  \smash{\begin{tabular}[t]{r @{}}
    #1%
  \end{tabular}}%
}}
\newcommand{\codefont}{\normalfont\ttfamily}
\newcommand{\setcode}[1]{{\codefont #1}}

\begin{document}

\begin{figure}
  \renewcommand{\arraystretch}{1.5}% Space code vertically (http://tex.stackexchange.com/a/31704/5764)
  \codefont% Set code font
  \begin{tabular}{p{\dimexpr\linewidth-2\tabcolsep}}
    \hline \\
    \string#include <stdio.h>
      \codecomment{include information about standard library} \\
    main()
      \codecomment{define a function named \setcode{main} \\
                   that receives no argument values} \\
    \string{
      \codecomment{statements of \setcode{main} are enclosed in braces} \\
    \qquad printf("hello, world\string\n");
      \codecomment{\setcode{main} calls library function \setcode{printf} \\
                   to print this sequence of characters; \\
                   \setcode{\string\n} represents the newline character} \\
    \string} \\\\
    \multicolumn{1}{c}{\normalfont A first C program.} \\
    \hline
  \end{tabular}
\end{figure}

\end{document}