I was reading a book and came across a program to read entries from a /proc
file.
The program which they mentioned has following line
printf("%.*s", (int) n, line);
I am not clear with meaning of above li开发者_StackOverflow中文版ne
- what type of print if above
"%.*s
used instead of%s
The code can be read here
Abstract from here:
.* - The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
So this prints up to n characters from line string.
The cast expression (int) n converts the value of n
to type int
. This is because the formatting specifier requires a plain int
, and I assume (since you didn't include it) the variable n
has a different type.
Since a different type, like size_t
might have another size, it would create problems with the argument passing to printf()
if it wasn't explicitly converted to int
.
精彩评论