Ayoub Chalghoum
Ayoub Chalghoum

Reputation: 11

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));

Upvotes: 1

Views: 92

Answers (1)

user142162
user142162

Reputation:

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.

Upvotes: 5

Related Questions