user2131316
user2131316

Reputation: 3287

what is the difference between "%02s" and "%2s" in C?

I have the following code:

char temp[32] = "";
sprintf(temp, "%02s", "A");

but it has warning as: Warning 566: Inconsistent or redundant format char 's', then I changed to code to: sprintf(temp, "%2s", "A");, the warning disappeared, what is the difference?

Upvotes: 2

Views: 8065

Answers (1)

unwind
unwind

Reputation: 400129

The %0 format means "0-padding", but you can't combine that with a string format specifier (s), that's undefined.

See the manual page:

0

The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined.

Upvotes: 7

Related Questions