Keil Logo

GENERAL: PROBLEMS USING '#' IN PRINTF/SPRINTF


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.03
  • C251 Version 2.14
  • C51 Version 5.50
  • C51 Version 6.00 Beta
  • C51 Version 6.00

SYMPTOMS

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

CAUSE

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.

RESOLUTION

Change your line of code to specify a width of four characters:

sprintf(buf, "#04bx", x);

MORE INFORMATION

Refer to a good ANSI C book, such as "The C Programming Language" by Kernighan and Ritchie for more information on the # flag.

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Tuesday, July 19, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.