Coding Help: Formatting Float Output in C

  • Thread starter Thread starter muzihc
  • Start date Start date
  • Tags Tags
    Float
Click For Summary

Discussion Overview

The discussion focuses on formatting float output in C programming, specifically addressing issues related to excess zeros and spacing in printed decimal values. Participants explore how to modify the code to achieve the desired output format.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in formatting float output and requests assistance with modifying their code to reduce excess zeros.
  • Another participant suggests using type casting to ensure that the division results in a float by changing the assignments to div1 = (float)int1 / 2.; and div2 = (float)int2 / 2.;.
  • A different participant mentions the use of the printf function and suggests changing the format specifier to %10f to limit the output to a specific width.

Areas of Agreement / Disagreement

Participants present multiple approaches to formatting float output, and while there is some agreement on the need for type casting, the discussion includes differing suggestions on how to format the output effectively. No consensus is reached on a single solution.

Contextual Notes

Some limitations include the potential misunderstanding of integer division in C, as well as the need for clarity on how format specifiers affect output. The discussion does not resolve these issues fully.

Who May Find This Useful

New C programmers, students seeking help with formatting output, and individuals interested in understanding type promotion and formatting in C.

muzihc
Messages
15
Reaction score
0
I'm very new to C. If I had a program like the one below, I was hoping someone could tell me how I should edit it so that the lines "printf("\n%20s%20f", "Half of 1", div1);" and "printf("\n%20s%20f", "Half of 2", div2);" output decimals without too many excess spaces. Currently when outputted, they are in decimal form but don't give remainders and have quite a few zeros. I'm really stuck here and have spent way too much time on this. I'd really appreciate any help.



#include<stdio.h>

int main(void)
{
int int1, int2;
float div1, div2;

printf("\nGive two integers: ");
scanf("%d%d", &int1, &int2);

div1 = int1 / 2
div2 = int2 / 2

printf("\n%20s%20d", "First Number", int1);
printf("\n%20s%20d", "Second Number", int2);
printf("\n%20s%20f", "Half of 1", div1);
printf("\n%20s%20f", "Half of 2", div2);

return 0;
}
 
Technology news on Phys.org
The division will be an integer division, change the code to:

div1 = (float)int1 / 2.;
div2 = (float)int2 / 2.;

Note if either number in a binary operation is a float, then the other number will be "promoted" to a float as well.

If you have a c reference manual, look up type promotion.
 
Thanks.
 
Here is another issue about "printf"
see http://www.google.com/search?hl=en&q=printf&aq=f&oq= for a detail reference.
If you want to limit the strengh of output. You could easily change the to

%10f which means you should limit the output to 10 bit spaces.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
47
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K