0

In this code:

sprint(buf_ptr, "%.*s", MAX_BUF_LEN, desc);

what does "%.*s", mean? what does "%20.20s" and "%.28s" mean- in snprintf?

4
  • In printf formatting a period . followed by a number is the "precision", which for strings means the maximum number of bytes printed. A * instead of a number for either precision or field width means that the number is read from an argument. Commented Oct 3, 2019 at 8:52
  • would you mind, could you give me example, to get clear picture? i define the size of MAX_BUF_LEN is 60. # define MAX_BUF_LEN 60, so how %20.20s and %.28s will do in snprintf? Commented Oct 3, 2019 at 8:55
  • 2
    Look up the documentation for printf, examples are plentiful Commented Oct 3, 2019 at 8:56
  • The "%20.20s" format will print a string with an output field 'width' of exactly 20 characters: adding leading spaces if the string is shorter than 20, truncating (from the right) if it has more than 20. Commented Oct 3, 2019 at 10:20

2 Answers 2

2

In the %*s format specification, the s indicates that the argument will be a (null-terminated) character string and the * (width specifier) says that the field width is given as the argument immediately preceding the string.

In your other examples, the width specifier(s) is(are) given as fixed values.

Actually, in the printf formats you give, there are both width and precision specifiers: the width is the value before the period and the precision is after. For strings, the width is the minimum output field size (space padded if necessary) and the precision is the maximum number of characters to print (string will be truncated, if necessary). In either case, if a * is specified for either, it will be assumed to be in the list of arguments (as an integer) immediately before the string it applies to.

Sign up to request clarification or add additional context in comments.

2 Comments

When a precision is given, the null character is not necessary.
@chux Not necessary only if the string array has more characters than the given precision. Otherwise, you can expect garbled garbage output.
1

what does "%.*s", mean?

desc, below, is a character pointer that need not point to a string1. Printing will continue until MAX_BUF_LEN charters (the precision) are printed or until a null character is read - which ever comes first.

sprint(buf_ptr, "%.*s", MAX_BUF_LEN, desc);

what does "%20.20s" ... mean- in snprintf?

Let us use two different values for clarity: "%19.21s".

desc is a character pointer that need not be a string. Printing will continue until 21 charters are printed or until a null character is read - which ever comes first. If the the number of charters to print is less than 19 (the minimum width), pad on the left with spaces to make at least 19 characters total.

sprint(buf_ptr, "%19.21s", desc);

what does ... "%.28s" mean- in snprintf?

Just like sprint(buf_ptr, "%.*s", 28, desc);


Loosely speaking, think of "%minimum.maximum s"


1 A string is a contiguous sequence of characters terminated by and including the first null character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.