ARM Technical Support Knowledge Articles

PROBLEMS USING '#' IN PRINTF/SPRINTF

Applies to: General Topics

Answer


Information in this article applies to:


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.

Article last edited on: 2005-07-19 10:28:45

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential