This question already has answers here:
Use of %d inside printf to print float value gives unexpected output
(4 answers)
Closed 2 years ago.
This is my code:
#include <stdio.h>
#define PI 3.141592f
#define RADIUS 10.0f
int main(void)
{
float volume;
volume = (4.0f/3.0f) * PI * (RADIUS * RADIUS * RADIUS);
printf("Volume: %d\n", volume);
return 0;
}
It's wrongly printing the value 536870912.
What am I doing wrong? As far as I know, the math is right.
%d is for printing int, so passing float to that invokes undefined behavior.
You should use %f or %g to print float.
Related
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}
Change your statement to (9.0/5.0)*c + 32 as 9 and 5 are integers, their division returns integer that is 1. So write them in float variable format.
This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
How to divide integers and get a float in C
(2 answers)
Closed 2 years ago.
I'm trying to write a basic programme, which gets a user's input, divides by seven, and then returns the result.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int s = get_int("Input: ");
float f = (s / 7);
printf("%f\n", f);
}
Eg. if I input "8", I would expect 1.142857. But I instead simply get "1.000000". Why is this?
#include <stdio.h>
int main()
{
int s=4;
float f = ((float)s / 7);
printf("%f\n", f);
getch();
}
You just have to typecast the int to float. int/float division give you the floor of given integer whereas float/float division gives you float
This question already has answers here:
C: printf a float value
(7 answers)
Closed 3 years ago.
I just made this code:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1416
int main (){
float x;
x = PI;
printf("x equals: %i.\n",x);
system("pause");
return 0;
}
and the 'Pi' number is 536870912. Can anybody tell me what is wrong?
You're using the %i format specifier for printf. This is for signed integers. Instead use %f as x is a float.
This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 4 years ago.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,i;
double abc,sum=0;
printf("Enter a value for x:");
scanf("%d",&x);
for(i=0;i<=6;i++)
{
abc=pow(1/2,i)*pow((x-1)/x,i+1);
sum=sum+abc;
}
printf("Sum is %f\n",sum);
}
As what i have checked there is no overflow of values in data type either or is it something else?
The issue is in this line of code:
abc=pow(1/2, i) * pow((x-1) / x, i + 1);
1/2 is always zero, and (x - 1)/x is also zero when x > 0. You can use 0.5 or 1.0 / 2.0 if you'd like to use a decimal value. Also, be careful about dividing by zero.
The resulting code would look like this:
abc=pow(0.5, i) * pow((x - 1.0)/x, i + 1.0);
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 7 years ago.
I have the following program:
float x = 3.e17;
printf("x = %f", x);
which gives the result:
x = 299999995292024832.000000
why is the result not 300000000000000000.000000?
#include <stdio.h>
#include <stdlib.h>
union
{
double d;
float f;
unsigned int ui[2];
} myun;
int main ( void )
{
float fx = 3.e17;
double dx = 3.e17;
printf("fx %f\n",fx);
printf("dx %lf\n",dx);
myun.f=fx;
printf("fx 0x%08X\n",myun.ui[0]);
myun.ui[0]++;
printf("fx %lf\n",myun.f);
myun.d=dx;
printf("dx 0x%08X 0x%08X\n",myun.ui[1],myun.ui[0]);
return(0);
}
(yes this is an incorrect/invalid way to use a union but it just happened to work)
fx 299999995292024832.000000
dx 300000000000000000.000000
fx 0x5C853A0D
fx 300000029651763200.000000
dx 0x4390A741 0xA4627800
wikipedia points out that single can handle up to 9 digits without a loss of precision and double 15-17. So right there is your answer, didnt necessarily need to do an experiment.
Because of limited float precision. Use double for more precision, but floating point is not always exact