0

I was wondering if it is possible to pass an integer as a precision specifier.

For example if

 static void ShouldBeTruncated( char * StringToCheck, int precision )
{
  (strlen( StringToCheck )>= precision) ? printf( "%.15s...", StringToCheck) : printf( "%s", StringToCheck)  ;
}

Is it even possible to replace %.15s with %.<<PRECISION GOES HERE >>s so the length of precision could be specified ? If it is possible, how to do it ?

Adrian Grzywaczewski
  • 868
  • 1
  • 12
  • 25

1 Answers1

2

Yes, use * as the precision, which takes another int argument off the argument list (before the argument whose value is to be printed):

void prec_print(double d, int prec)
{ 
    printf("%.*f\n", prec, d);
}

(Similarly, the field width may be specified dynamically by using *, too: %*.*f, width, prec, val.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084