Meaning of "." in printf

It specifies the "Character String Maximum field width"

The precision within a string format specifies the maximum field width:

%2.6s

specifies a minimum width of 2 and a maximum width of 6 characters. If the string is greater than 6 characters, it will be truncated.


In %.*s, the .* limits the number of bytes that will be written. If this were written with a numeral included, such as %.34s, then the numeral would be the limit. When an asterisk is used, the limit is taken from the corresponding argument to printf.

From C 2011 (N1570) 7.21.6.1 4, describing conversion specifications for fprintf et al:

An optional precision that gives … the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero.

Tags:

C