Home > Mobile >  Prevent Increasing Value from Pushing Another
Prevent Increasing Value from Pushing Another

Time:03-03

Not sure how to phrase my question, sorry for the confusing title. Basically, I have two variables being displayed (score and high score) that increase as a user plays the game I am making.

    printf("Score: %d", score);
    printf("%s", SPACE);
    printf("High Score: %d", highScore);

When score increases as you play, it starts to push over high score beyond my "game board". I'm sure there's a basic fix for this, but I am new. How should I prevent this from happening?

CodePudding user response:

You can limit the length of a string using a precision specifier in printf(). However, you're printing integers, for which printf() does not allow a "maximum length" specifier.

asprintf()

To get around this, you could do the integer conversion before passing to printf() by using asprintf():

char *score_str, *highScore_str;
asprintf(&score_str, "%d", score);
asprintf(&highScore_str, "%d", highScore);

printf("Score: %.5s", score_str);
printf("%s", SPACE);
printf("High Score: %.5s", highScore_str);

free(score_str);
free(highScore_str);

where 5 is the precision specifier—you can use any other positive number. Note that free()ing the strings is very important: asprintf() dynamically allocates memory for those strings, and you (probably) don't want to leak memory on every print.

snprintf()

Dynamically allocating memory here is unnecessary here, though, because we already know how long we want the strings to be. Using snprintf(), we can eliminate the need for asprintf() and the printf() precision specifier:

char score_str [6];
char highScore_str [6];

snprintf(score_str, 6, "%d", score);
snprintf(highScore_str, 6, "%d", highScore);

printf("Score: %s", score_str);
printf("%s", SPACE);
printf("High Score: %s", highScore_str);

Note that we use 6 in both the declarations and snprintf() to account for the terminating NUL.


Both code snippets produce identical output, such as this:

Score: 5 High Score: 12345

where score = 5 and highScore = 123456789.

printf() with a double argument

The above format has a problem: the scores are simply truncated (the remaining digits are deleted). This can be remedied with scientific notation. e and E are the printf() format specifiers for scientific notation, but g or G are probably preferred here, as they use simple decimal notation for small numbers.

printf("Score: %.5G", (double)score);
printf("%s", SPACE);
printf("High Score: %.5G", (double)highScore);

will produce output like

Score: 5 High Score: 1.2346E 08

With .5 as the precision, we can guarantee that each number will not exceed 10 characters: 5 for the number score, 1 for the decimal, and 4 for the scientific notation.

Depending on the nature of your game, such a format may not be reasonable.


When confronted with the problem of fitting a number into a limited number of characters, there are always two options.

  1. Truncate it, as we did in the first two examples.

  2. Use a special notation, as we did in the third.

There's another way to look at option #2: use suffixes such as k, M, and B when the score gets too long. printf() does not provide such functionalityy, so you would have to implement that system (which I assume is the most desirable) yourself.

  •  Tags:  
  • c
  • Related