Showing piece of code line by line

beamer

I want to show some program code line by line. I have the following code:

\documentclass[10pt,english]{beamer}

\usepackage{listings}
\lstset{
    language=C,
    basicstyle=\ttfamily
}

\begin{document}
\begin{frame}[fragile]
\frametitle{Slides with code}
\begin{itemize}
\item <1-> A piece of  code
\item[] <2-> \begin{lstlisting}
int main(void)
\end{lstlisting}
\item[] <3-> \begin{lstlisting}
{
\end{lstlisting}
\item[] <4-> \begin{lstlisting}
   int i;
\end{lstlisting}
\end{itemize}
\end{frame}

\end{document}

Now this is error-prone a painstaking. Is there another way to produce these slides?

Best Answer

You can use mathescape to insert appropriate \pauses within the code, resulting in an easier way to negotiate the code reveal:

enter image description here

\documentclass{beamer}

\usepackage{listings}
\lstset{
  language=C,
  basicstyle=\ttfamily
}

\begin{document}

\begin{frame}[fragile]
  \frametitle{Slides with code}

\begin{lstlisting}[caption=,mathescape]
int main(void)
$\pause${
$\pause$   int i;
$\pause$}
\end{lstlisting}

\end{frame}

\end{document}