Characters with accents, such as á, é, í, ó, ú, which I use because I work with the Spanish language, can be displayed correctly in the listings using the following configuration:
\lstset{
literate=%
{á}{{\'a}}{1}
{é}{{\'e}}{1}
{í}{{\'i}}{1}
{ó}{{\'o}}{1}
{ú}{{\'u}}{1}}