'C' Programming- Format string help.

CM4

Member
Jun 9, 2001
64
0
0
What would be printed by the code fragment given below? Please could someone explain the significance of the 4 in the formatting string %4.2f.

float x;

x = 137.9397;
printf ("%4.2f" , x);

Thanks.
______________________________________________________________________________

Also just a quick side question, is the following definition valid?

int **p;

If it is could you please explain why? If not why?

I REALLY appreciate all this help.

Thankyou. :)
 

br0wn

Senior member
Jun 22, 2000
572
0
0
What would be printed by the code fragment given below? Please could someone explain the significance of the 4 in
the formatting string %4.2f.

float x;

x = 137.9397;
printf ("%4.2f" , x);

Thanks.


Result would be : 137.93

The format %4.2f means that you specify only to use 2 decimal digits
and 4 is the width (usually right-justified).
If you have a number = 3.223, printing with %5.2f
will generate : _3.22 ( the symbol _ is a blank symbol, thus
the number will occupy 5 spaces-- dot is counted as 1 space)

Also just a quick side question, is the following definition valid?

int **p;

If it is could you please explain why? If not why?


Yes, it is valid.
It means that you define p to be a pointer that will hold address/pointer (instead of data).
That definition can be used to generate 2D array.
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
What would be printed by the code fragment given below?

I get 137.94 because of rounding.