0

Hi, i am absolute newbie in programming. I am starting with learning C by book "C Programming Language (2nd Edition)" and stuck in very first example where we get exercise to write simple program that prints values of temperatures from lower to upper in 2 columns (tabs) that contains Celsius a Fahrenheit.

I'v get problem because trying to edit those code for:

  1. Celsius is main system.
  2. Steps measured dynamically by dividing lower on any given number.

And all work perfectly while i am using integers variables.

#include <stdio.h>

main()
{
    int celcius, farenheit;
    int lower, upper, step;

    lower = -273.15;
    upper = 0;
    step = lower / -10; // Dividing lower temperature by given number

    celcius = lower;

    while (celcius <= upper) {
        farenheit = celcius * 9/5 + 32;
        printf("%d\t%d\n", celcius, farenheit);
        celcius = celcius + step;
    }
}

But goes to absolutely random numbers when i try using float or double variables for more precise result: (There is code and output in terminal)

#include <stdio.h>

main()
{
    float celcius, farenheit;
    float lower, upper, step;

    lower = -273.15;
    upper = 0;
    step = lower / -10; // Dividing lower temperature by given number

    celcius = lower;

    while (celcius <= upper) {
        farenheit = celcius * 9/5 + 32;
        printf("%d\t%d\n", celcius, farenheit);
        celcius = celcius + step;
    }
}

Output:

1610612736      1073741824
1073741824      1073741824
-1073741824     1073741824
1073741824      536870912
-1073741824     536870912
1073741824      0
-2147483648     0
-2147483648     -2147483648
536870912       -1610612736
-2147483648     0

So what happened behind that number magic and how to get this to work?

Ablix Corner
  • 3
  • 1
  • 3
  • 1
    You should post the code that causes the erroneous output (it seems from your text as if the output is not from the code you posted) – M.M Nov 12 '18 at 03:00
  • regarding: `main()` there are only two valid signatures for `main` they are `int main( void )` and `int main( int argc, char * argv[] )` – user3629249 Nov 12 '18 at 03:04
  • regarding: `lower = -273.15; upper = 0; step = lower / -10;` this is dividing a negative number by a negative number. The result will be positive – user3629249 Nov 12 '18 at 03:06
  • please do not be editing the code in response to comments. Rather, add a 'EDIT' section that contains any edited code – user3629249 Nov 12 '18 at 03:08
  • what 'magic' number? – user3629249 Nov 12 '18 at 03:11
  • 1
    regarding: `printf("%d\t%d\n", celcius, farenheit);` the variables `celcius` and `farenheit` are `float` values. so the `format string` should be using `%f`, not `%d` – user3629249 Nov 12 '18 at 03:12
  • @user3629249 it is fine to edit the question in response to comments requesting clarification. Anyone can check the edit history if they care to do so. The question should NOT say "EDIT:" in it, this site is supposed to be a knowledge base where the question stands alone as a question, not as a series of edits – M.M Nov 12 '18 at 03:36
  • @M.Mm When the OP edits their code in response to some comment, then the comment no longer makes sense.. This leads to confusion, especially for new programmers, who will (often) not understand the connection between the comment and the question – user3629249 Nov 13 '18 at 19:24

2 Answers2

1

Two problems: first of all, you are doing integer division which causes your quotient to be truncated. Multiply in your calculations by 9./5., not 9/5. The former gives the actual result, but the latter performs integer division

Your second problem is using %d as your format specifier. You need %f which is for float. Read the man pages for printf for more details.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • Thanks, i don't even knew what "%d" means, this fixed my problem, now i will read part of manual about format specifier's. – Ablix Corner Nov 12 '18 at 03:14
  • when `celcius` is a `float`, the code `celcius * 9/5 + 32` is correct, the associativity is `(celcius * 9) / 5` etc. – M.M Nov 12 '18 at 03:38
-1

hope this can help you

#include <stdio.h>

int main(int argc,char **argv)
{
    double celcius, farenheit;
    double lower, upper, step;

    lower = -273.15;
    upper = 0;
    step = lower / -10; // Dividing lower temperature by given number

    celcius = lower;

    while (celcius <= upper) {
        farenheit = celcius * 9/5 + 32;
        printf("%5.2f\t%5.2f\n", celcius, farenheit);
        celcius = celcius + step;
    }
    return 0;
}
  • That also help, and giving result without lots of numbers after the comma. – Ablix Corner Nov 12 '18 at 03:18
  • 4
    it looks like your answer helped the OP, but consider adding some explanatory notes along with your code; now that the OP has replied, you can even address your notes to the OP's delight about "without a lot of numbers after the comma"; in other words, it sounds like the OP is not yet familiar with the options for using the `%f` modifier in `printf()`; you can explain why you wrote that line the way you did, and link to useful documentation for `printf()`; TL; DR : please explain your code for the OP, who is just beginning with C – landru27 Nov 12 '18 at 03:27