I know very little about C, but am trying to understand a programme written in it.
I've come across this line, but am unsure what it means:
fprintf(kinsh.fp, "%-.*lg", ndigits, kins[i+j*n]);
I think this means "Print a number with ndigits decimal places or significant figures (whichever is shorter) to kinsh (which is a file). However, I don't know what kins, which is an array, is doing in there.
Have I misunderstood - in context, it would make more sense if the line is reading from kinsh and writing into kins?
The fprintf function is defined as follows :
int fprintf ( FILE * stream, const char * format, ... );
So, kinsh.fp
is the FILE
pointer.
Now the format string "%-.*lg"
is a C format string which implies this :
-
implies left alignment.
implies the precision.*
implies that the precision is not hardcoded but provided as a parameter which is the first parameter after the format string i.e. ndigits
.lg
will be treated by C99 compiler as double
, Lg
is for long double. (As mentioned by @mafso in comments, lg
is Undefined Behaviour in C89)kins[i+j*n]
.Edit
What is does :
The statement writes a long double
value stored at kins[i+j*n]
, with ndigits
precision and left alignment, in the file pointed by kinsh.fp
.
The general format string format is : %[parameter][flags][width][.precision][length]type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With