ARM Technical Support Knowledge Articles

LIMIT STRING OUTPUT IN PRINTF USING %S

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

I am using the following sprintf statement to limit a string output. However it outputs more than the 5 characters specified. In fact, it even outputs a new line (' ') sequence that is part of str.

  sprintf (buf, "string = %5s", str)

Is this a problem with the Keil C Compiler?

ANSWER

No, this is standard ANSI behaviour. You need to specify a precision (number after the dot '.') in the %s format parameter to limit size of the string printed. The precision field is a non-negative number that specifies the number of characters to print.

Example:

  sprintf (buf, "string = %.5s", str)

Using this format string, the string passed by str will be truncated after five characters.

MORE INFORMATION

Article last edited on: 2005-07-15 09:52:59

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