Jump to content
• Advertisement

Public Group

# sprintf %% escape sequence

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hey guys, I'm having issues using the literal text "%". Say I want to type a creature's health in percent, ie "100%". Shouldnt using the escape sequence "%%" create a literal percentage sign? Here's my code:
sprintf_s( strHealth, sizeof(strHealth), "%d %%", CreatureHealth);


I get a pop-up box runtime error saying
     Debug Assertion Failed!
Program: c:\Users\Matt\Desktop\Game\Debug\Game.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line: 2293

Expression: ((state == ST_NORMAL) || (state == ST_TYPE))

I get assertion errors for a lot of (usually trivial) things and so finding data relevant to this specific instance has proved a tad challenging for me, especially as google/gamedev searches ignore percentage signs! Any help is greatly appreciated!

#### Share this post

##### Share on other sites
Advertisement
Yes, %% should create a literal percentage sign. Two things though: First, ensure that strHealth is pointing to valid contiguous bit of memory. Second, remember that sizeof only works on ARRAYS. NOT pointers.
/* this works: */char strHealth[10];sizeof(strHealth);/* this doesn't work: */char * strHealth = /* whatever */sizeof(strHealth); /* BAD *//* this doesn't work either */void function( char strHealth[] ){ sizeof(strHealth); /* BAD */ }

#### Share this post

##### Share on other sites
nobodynews: Thankyou, I am not using pointers, but I appreciate your feedback.

I have tested different code to verify where the issue lies and also to attempt to clarify the error somewhat:
/* sizeof() and the input integer have been replaced with hard coded versions to show they are not the issue*//* This code works perfectly (outputs '10')*/      char creaturehealth[20];     sprintf_s( creaturehealth, 20, "%d", 10);/* Addition of %% gives the error */      char creaturehealth[20];     sprintf_s( creaturehealth, 20, "%d %%", 10);

Omitting the '%' isn't such a horrible solution.. but its inclusion seems trivial.

#### Share this post

##### Share on other sites
Does the real sprintf work? It could be a bug in Microsoft's "safe" implementation. I use %% all the time and have never had problems.

#### Share this post

##### Share on other sites
What version of Visual Studio are you using? The following works perfectly for me in Visual Studio 2008 SP1:

#include <stdio.h>#include <iostream>int main(int argc, char* argv[]){    char buffer[20];    sprintf_s(buffer, "health: %d", 10);    std::cout << buffer << std::endl;    sprintf_s(buffer, "health: %d%%", 10);    std::cout << buffer << std::endl;    return 0;}

Output is:

health: 10health: 10%

Can you run my test program and tell me what happens? Perhaps the problem is somewhere else?

#### Share this post

##### Share on other sites
Thankyou gekko and Codeka for the replies.

gekko: sprintf produces the same error, so no luck there.

Codeka: I am using Visual C++ 2008 Express Edition. Your test program ran perfectly, the output is identical to yours. How could this specific project mess with sprintf's proper execution? I'm afraid I'm confused!

After some more testing and googling, you also get this error if you don't escape the % sign. It's as if it is interpreting my "%%" as two separate "%".

Thanks,

#### Share this post

##### Share on other sites
It's rather strange that one works and other does not. I would suggest the next step would be to run the program in a debugger, and when it hits that assertion, jump in and have a look at what it's doing. For example, you can look at the parameter passed in for the format to make sure it's what you expect, see what the state variable actually is (that is, the assertion is checking whether state is ST_NORMAL or ST_TYPE -- obviously, it's something else, what it is)? And so on...

But clearly, it is possible to get it working.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 11
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632952
• Total Posts
3009438
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!