\usepackage{listings}

\lstset{language=C,
        basicstyle=\ttfamily,
        keywordstyle=\color{blue}\ttfamily,
        stringstyle=\color{red}\ttfamily,
        commentstyle=\color{green}\ttfamily,
        morecomment=[l][\color{magenta}]{\#}}
        
% Do everything above in your preamble

\begin{lstlisting}
    if (obstacles[i + j*nx]) tot_u = 0;
    else tot_u = sqrt(u_sq);
\end{lstlisting}