The g and G type specifiers behave either like the f type conversion or the e and E type conversions, depending on the value to be converted, as follows:
printf( "Use of the %%g, and %%G format specifiers:\n" );
printf( " Specifier %%18.12 g%%18.3g" );
printf( " %%18.3G %%18g\n" );
double k = aDouble * 1e-15;
for( int i = 0 ; i < 10 ; i++, k *= 1000 )
printf( " [%18.12g][%18.3g][%18.3G][%18g]\n" ,
k , k , k , k );
The value of k is assigned aDouble multiplied by a very small number, giving a very small number. By using a loop and multiplying k by 1,000, various g or G type conversions are used in each iteration as the value increases. We will see that the g or G type conversions attempt to format the value in the shortest format possible for the given value. This will be clear in the program's output.
Enter these code snippets into double.c. Compile and run...