• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

'C' Programming- Format string help.

CM4

Member
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. 🙂
 
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.
 
Back
Top