#include <stdio.h>
int main(void)
{
float with;
float inacbal;
float acleft;
scanf("%f",&with);
scanf("%f",&inacbal);
if((with%5)==0)//error here
{
acleft=inacbal-with-0.50;
printf("%f",acleft);
}
else
printf("%f",inacbal);
return 0;
}
-
5It means you can't do a modulo of a float by an int. – Medinoc Sep 18 '14 at 13:46
-
1Please format code as code, not as a quotation. – Oliver Charlesworth Sep 18 '14 at 13:47
-
`%` is an integer operator. `with` is a `float`. – lurker Sep 18 '14 at 13:50
-
2The C standard 6.5.5: "The operands of the % operator shall have integer type." Your program does not conform to the C standard, so you get a compiler error. – Lundin Sep 18 '14 at 14:02
-
It simply means that you cannot have a float as an operand for the %(mod) operation. – Abhishek Choubey Sep 18 '14 at 14:12
2 Answers
float with;
if((with%5) == 0)
is incorrect. You can apply %
only to integers. If you really want to do a modulo operation on float
, then use fmod or if you're not bothered about the sign of the remainder, then use the new IEEE 754r mandated C99's remainder. From Sun's Numerical Computation Guide:
The remainder(x,y) is the operation specified in IEEE Standard 754-1985. The difference between remainder(x,y) and fmod(x,y) is that the sign of the result returned by remainder(x,y) might not agree with the sign of either x or y, whereas fmod(x,y) always returns a result whose sign agrees with x.

- 31,634
- 25
- 118
- 222
-
-
1@Dumb does `with` have to be a float? Can it be an `int`? If it needs to be a float, then look at `fmod`. – lurker Sep 18 '14 at 13:51
-
-
Actually in order to avoid typecasting in this line acleft=inacbal-with-0.50;.I am putting all variables in float otherwise there will be some precision error – Dumb Sep 18 '14 at 13:58
-
Use the right data types that makes sense for what quantity the variable denotes; then type case only when you need to. Say you could do `acleft = inacbal - (float) with - 0.5f;`. – legends2k Sep 18 '14 at 14:01
-
@Dumb Making `with` a `float` instead of an `int` if it really is integer does not help with precision in the `aclef = inacbal - with - 0.50` calculation. A certain amount of precision error will occur with `float` regardless. – lurker Sep 18 '14 at 14:04
-
If I"LL just put float acleft and other in integers then won't there be any precision loss? – Dumb Sep 18 '14 at 14:10
-
If you're measuring quantities which can be only integers (discrete) then use `int` e.g. would be number of _people_, _keys on a keyboard_, _columns on a terminal window_, _files on a machine_, etc. If you want to measure quantities that can be fractions (continuous) then use `float` (or `double` depending upon the precision you want) e.g. _volume_, _length_, _money_, etc. – legends2k Sep 18 '14 at 14:17
You are getting that error because you can't use the modulus operator (%
) with float
.
If you want to calculate the remainder of it,use fmod()
like this:
fmod(with,5);
fmod
will return the remainder of the division. Don't forget to include math.h
in order to use fmod
.

- 20,225
- 7
- 37
- 83