www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/19/22:54:23

From: Andrew Crabtree <andrewc AT typhoon DOT rose DOT hp DOT com>
Message-Id: <199707200253.AA059677223@typhoon.rose.hp.com>
Subject: Re: Long Question-Don't take a long time to answer
To: thedarkage AT mail DOT geocities DOT com (Guilherme Silveira)
Date: Sat, 19 Jul 1997 19:53:42 PDT
Cc: djgpp AT delorie DOT com
In-Reply-To: <3.0.32.19970719140218.006ec000@mail.geocities.com>; from "Guilherme Silveira" at Jul 19, 97 9:48 pm

and I got an the folowing (2), do you know why?
Yes - the code you are not showing us has bugs in it ;)

> float a=0;
> float b=0;
> float c=0;
> a=375;
> b=150;
> c=a/b;

> (2)
> I got: c=2
If you enter just the code you have entered here gcc will optimize out all of 
the variables and division, and just push an immediate value of 2.5 on 
the stack (assuming you call printf or something).

> This return is 'cause '/' returns only an integer. (I think so).
Nope.  Not unless you are using integer values or are coercing the value with
(int) a/b).

try this 

#include <stdio.h>
int main(void) {
float a,b,c;
printf("Enter a float\n");
scanf("%f",&a);
printf(Enter another float\n");
scanf("%f",&b);
c = a/b;
printf("float 1/float 2 = %f\n",c);
return 0;
}

Andrew









- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019