Registered User
Registered User

Reputation: 5371

typecasting syntax not clear

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 line

  1. what type of print if above "%.*s used instead of %s

The code can be read here

Upvotes: 3

Views: 142

Answers (2)

unwind
unwind

Reputation: 399863

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.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

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.

Upvotes: 6

Related Questions