Pelles C forum

C language => Beginner questions => Topic started by: Marten on December 30, 2006, 07:43:20 PM

Title: divisions
Post by: Marten on December 30, 2006, 07:43:20 PM
How can i divide 4/8 in c or 3/2 or things like that cause i always get 0.0000 when i use the following code:

float a=4/8;
printf("%f",a);

!?!?!? :cry:
Title: divisions
Post by: TimoVJL on December 30, 2006, 08:31:37 PM
Code: [Select]

#include <stdio.h>
#include <math.h>

int main(int argc,char **argv)
{
float a;
a=4/8;
printf("%f\n",a);  //0.000000
a=4.0/8;
printf("%f\n",a);  //0.500000
a=(float)4/8;
printf("%f\n",a);  //0.500000
return 0;
}
Title: divisions
Post by: cane on December 30, 2006, 08:33:39 PM
Either:
Code: [Select]

float a=4.0/8.0;
printf("%f",a);

Code: [Select]

float a=(float)4/8;
printf("%f",a);
Title: divisions
Post by: Marten on December 30, 2006, 09:07:38 PM
Thanx !!!