GENERAL: PROBLEMS USING '#' IN PRINTF/SPRINTF
Information in this article applies to:
I'm experiencing trouble using sprintf with an unsigned char variable. The program hangs when I use the # flag with a bx variable type. When I remove the #, the sprintf function executes normally.
Here is my code:
unsigned char x = 0xAA; sprintf(buf, "#02bx", x);
You are trying to print '0xAA' which is four characters, however you have specified a width of only two characters. Sprintf crashes as a result.
Change your line of code to specify a width of four characters:
sprintf(buf, "#04bx", x);
Refer to a good ANSI C book, such as "The C Programming Language" by Kernighan and Ritchie for more information on the # flag.
The following Discussion Forum threads may provide information related to this topic.
Last Reviewed: Tuesday, July 19, 2005
of your data.