Sjak M
Sjak M

Reputation: 63

Hide integer in sprintf

I'm trying to print nothing when my bool (fps) is set to false. At the moment it prints 0, because I have no other idea how I would disable the integer from displaying.

sprintf(drawdev, "%s%d", (fps) ? "FPS: " : "", (fps) ? framecount : 0);

How can I make it so the integer doesn't display, just like the "FPS: " text?

Upvotes: 0

Views: 239

Answers (3)

David Ranieri
David Ranieri

Reputation: 41065

You can use the precision modifier %.*d, passing 0 will have no output:

sprintf(drawdev, "%s%.*d", (fps) ? "FPS: " : "", !!fps, (fps) ? framecount : 0);

Upvotes: 0

dbush
dbush

Reputation: 225827

The cleanest way to do this is to get rid of the ternary and use an if:

if (fps) {
    sprintf(drawdev, "FPS: %d", framecount);
} else {
    sprintf(drawdev, "");
}

Upvotes: 2

UliB
UliB

Reputation: 53

I would do this:

fps?sprintf(drawdev,"FPS: %d",framecount):sprintf(drawdev,"FPS:");

Upvotes: 0

Related Questions