简体   繁体   中英

How can I print a number into a char array?

I'm developing a pacman game with allegro (in Ubuntu) but I am unable to show the score. I found this code but it doesn't work. Could you help me please?

char scoretxt[10];
printf(scoretxt,"score: %d",score);
textout_ex(buffer, font, scoretxt, TILE_SIZE*(MAP_WIDTH)*3/4, TILE_SIZE, makecol(255,255,255), makecol(0,0,0));

You should be using snprintf , not printf :

snprintf(scoretxt, 10, "score: %d", score);

Here the 10 is the length of the buffer scoretxt to ensure that snprintf doesn't write outside the allocated array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM