开发者

typecasting syntax not clear

开发者 https://www.devze.com 2023-02-19 04:55 出处:网络
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

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

  1. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消