1

Suppose I use the following printf() specifier string:

printf("%.*s", length, str);

which asks printf() to print the first length characters in str. The question is which type length has? I only see brief mentions of integer number in the documentation.

So it looks like it is int. Then it looks like I cannot use the following code when a string is very long:

const char* startOfString = ...    
const char* middleOfString = ...
printf("%.*s", (int)( middleOfString - startOfString ), startOfString);

It looks like I cannot output more than INT_MAX characters this way.

So which type is precision? Is it int or is it size_t or anything else?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • If by "the documentation", you mean "cplusplus.com", I suggest you find a better default documentation source. I'd start with the manpage (`man fprintf`) on a unix-like system ("the next argument... which must be of type int"). If you're on Windows, you can read [manpages online](http://man7.org/linux/man-pages/man3/fprintf.3.html). There's also http://en.cppreference.com/w/c/io/fprintf . (" the precision is specified by an additional argument of type int") – rici Mar 30 '15 at 05:02

1 Answers1

4

It's an int, and you cannot print more than INT_MAX characters that way.

From the C standard, § 7.21.6.1, para. 5:

a field width, or precision, or both, may be indicated by an asterisk. In this case, an int argument supplies the field width or precision.

rici
  • 234,347
  • 28
  • 237
  • 341