0

I'm currently reading "The C Programming Language - 2nd Edition". In the first chapter, it is explained that an operation of an float with an int results an int. There is this program:

#include <stdio.h>

int main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;
    while (fahr <= upper)
    {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f\t%6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}

When the line fahr = fahr + step is executed, shouldn't fahr become an int ? Does it stays a float because I was declared as a float ?

0poss
  • 153
  • 1
  • 8
  • 2
    I think you must have misread. When you combine float and int, the int is converted to float, not the other way around. – Barmar Feb 14 '19 at 21:19
  • [this question](https://stackoverflow.com/questions/45416000/why-is-the-sum-of-an-int-and-a-float-an-int) may be relevant. – Barmar Feb 14 '19 at 21:22
  • Variables can't change type in C. – Carey Gregory Feb 14 '19 at 21:23
  • OT: regarding: `celsius = (5.0/9.0) * (fahr-32.0);` the variables (celsius, fahr) have type `float`, but the literals (5.0, 9.0, 32.0) have type `double` A much better way is to declare the literals as type `float` by appending a `f` to the end of each literal, I.E. `celsius = (5.0f/9.0f) * (fahr-32.0f);` – user3629249 Feb 16 '19 at 06:19

4 Answers4

2

Yes, if you declared your variable as a float, it won't change in your code. If you do an operation between an int and a float and put on a float variable, you will have a float result, and the opposite is true, if you put your variable in a int var, you will lose the decimal part of your number.

You can't change your variable type in C.

R. C. W.
  • 96
  • 5
  • 1
    However, in the actual code the compiler will promote the `int` to `float` while performing the actual calculation – user3629249 Feb 16 '19 at 06:21
0

If the book said that, it is wrong. Simple as that!

When you add an integer to a float, you get a float. Furthermore, you assigned the result to a float, so it can't be anything else. Objects don't change type.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Since you declare fahr as a float, any value you assign to it will be converted to float.

Any arithmetic operation between an int and a float will have a float result. This is specified as part of the usual arithmetic conversions:

6.3.1.8 Usual arithmetic conversions

1 Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

If both operands have the same type, then no further conversion is needed.

Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
62) For example, addition of a double _Complex and a float entails just the conversion of the float operand to double (and yields a double _Complex result).

C 2011 Online Draft

An arithmetic operation between two ints will yield an int result. For example, 1/2 yields 0, 4/3 yields 1, 7/3 yields 2, etc. If you assign the result of an integer division to a float variable, it will be stored as a float, but you don't get the fractional portion of the result. IOW, given code like

float fahr = 4 / 3;
printf( "%f\n", fahr );

your output will be 1.0, not 1.33333. If you want a floating-point result, at least one of the operands must be a floating-point type:

float fahr = 4 / 3.0f;
printf( "%f\n", fahr );

will output 1.33333.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-3

+1 For reading that book. C chooses the highest resolution on arithmic, so float wins in your case.

Willy65
  • 1
  • 1