Coding Help: Formatting Float Output in C

  • Thread starter muzihc
  • Start date
  • Tags
    Float
In summary, to output decimals without excess spaces, you can change the code to use float variables and use printf with a specified number of spaces, such as "%10f", to limit the output length. Additionally, type promotion can ensure proper division with float values.
  • #1
muzihc
16
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
  • #2
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.
 
  • #3
Thanks.
 
  • #4
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.
 

FAQ: Coding Help: Formatting Float Output in C

What is "Formatting Float Output" in C?

Formatting Float Output in C refers to the process of controlling the way floating-point numbers are displayed in a program. This includes specifying the number of decimal places, adding leading zeros, and choosing between scientific notation or fixed notation.

Why is it important to format float output in C?

Formatting float output in C is important for several reasons. Firstly, it improves the readability of the program for both the programmer and the user. Secondly, it ensures that the output is presented in a consistent and organized manner. Lastly, it allows for more precise control over the display of floating-point numbers.

How do I format float output in C?

To format float output in C, you can use the printf() function. This function takes in a format specifier, which determines how the floating-point number will be displayed. For example, %f is used for fixed notation, and %e is used for scientific notation. You can also specify the number of decimal places by adding a precision value after the format specifier.

Can I use formatting flags with float output in C?

Yes, there are several formatting flags that can be used with float output in C. These include adding leading zeros (%0), specifying the field width (%n), and aligning the output to the left or right (%- or %%). These flags can be used in combination with the format specifier to customize the display of floating-point numbers.

Are there any common mistakes when formatting float output in C?

Yes, one common mistake when formatting float output in C is forgetting to include the correct format specifier. This can result in unexpected or incorrect output. It is also important to pay attention to the precision value, as using a value that is too small can lead to rounding errors. Additionally, using the incorrect formatting flags or forgetting to include them can also cause issues with the output.

Back
Top