-2

I'm trying to run some basic code in C to declare 2 float variables, and then divide them and put that value in the 3rd variable. After this I print all 3.

#include <stdio.h>
int main ()
{
  /* variable definition: */
  float a, b, c; 
  /* variable initialization */
  a = 1.2;
  b = 2.7;  
  c = a / b;
  printf("Floats (a,b) and quotient (c) are : %d,%d,%d \n", a,b,c);  
   return 0;
}

I'm using the online compiler "www.ideone.com" to compile and run the code, and this is the result I'm getting:

Success time: 0 memory: 2156 signal:0

Floats (a,b) and quotient (c) are : 1073741824,1072902963,-1610612736

Can anyone see if perhaps I made a mistake in the code? It's for a class and everything worked fine for every step of directions until I changed from int to float.

gsamaras
  • 71,951
  • 46
  • 188
  • 305

2 Answers2

1

You want to print floats, thus change this:

printf("Floats (a,b) and quotient (c) are : %d,%d,%d \n", a,b,c);

to this:

printf("Floats (a,b) and quotient (c) are : %f,%f,%f \n", a,b,c);

For more, check the ref.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

%d is not a correct conversion for float, use %f instead

change:

printf("Floats (a,b) and quotient (c) are : %d,%d,%d \n", a,b,c);

to:

printf("Floats (a,b) and quotient (c) are : %f,%f,%f \n", a,b,c);

and you should be fine.

Charlestone
  • 1,248
  • 1
  • 13
  • 27