# Russian language in lstlisting

I am using LaTeX on overleaf.com and trying to use russian language in code, but the compiler gives an error "Invalid UTF-8 byte sequence".

I can use russian text freely out of code scopes.

My LaTeX code is:

\documentclass{article}

\usepackage[T2A]{fontenc}
\usepackage[english, russian]{babel}

\usepackage{listings}

\lstset{
language=C++
}

\begin{document}

\begin{lstlisting}
int x = 3;
//привет1
\end{lstlisting}

привет2

\end{document}


So after I compile my program I see no russian words in code:

How is it possible to use russian language in code?

• Welcome to TeX.SE. How is the input file encoded? Is it utf8? – Mico Apr 24 at 13:30
• @Mico I think yes, I can add line \usepackage[utf8]{inputenc} if it is necessarry. – elo Apr 24 at 13:53

You can use literate. The can find the command names in t2aenc.dfu.

\documentclass{article}

\usepackage[T2A]{fontenc}
\usepackage[english, russian]{babel}

\usepackage{listings}

\lstset{
language=C++,
literate={п}{\cyrp}1
{и}{\cyri}1,
}

\begin{document}

\begin{lstlisting}
int x = 3;
//пи
пи
\end{lstlisting}

привет {\itshape привет}

\end{document}