1

The code is:

#include <stdio.h>
#include <stdlib.h>

int main() {
    double C, F;
    printf("Enter the temperature in Celisius: ");
    scanf("%f", &C);
    F = 32 + (C * (180.0 / 100.0));
    printf("%f C = %f F\n", C, F);
    system("pause");
    return 0;
}

The output is:

Enter the temperature in Celisius: 100
-92559604910177974000000000000000000000000000000000000000000000.000000 C = -166607288838320360000000000000000000000000000000000000000000000.000000 F

Toby Speight
  • 23,550
  • 47
  • 57
  • 84
Ambitions
  • 1,729
  • 2
  • 8
  • 21
  • You are using the wrong format specifier. `double`s are printed with `%lf` – StoryTeller - Unslander Monica Mar 07 '17 at 06:22
  • 1
    @StoryTeller is it? :) – Sourav Ghosh Mar 07 '17 at 06:25
  • @SouravGhosh - C99 allowed it, I assume so that calls to printf and scanf will match, so one really ought to use it. – StoryTeller - Unslander Monica Mar 07 '17 at 06:32
  • 1
    @StoryTeller for `printf()` family, `l` length modifier has no effect on `f` conversion specifier. – Sourav Ghosh Mar 07 '17 at 06:33
  • @SouravGhosh - [That change is explicitly mentioned in the revision to the second edition](http://port70.net/~nsz/c/c11/n1570.html#Forewordp7). And again, this is for consistency. Using different format specifiers contributes to precisely this type of mistakes. – StoryTeller - Unslander Monica Mar 07 '17 at 06:35
  • 1
    You should compile with all warnings and debug info (i.e. `gcc -Wall -g` if using [GCC](http://gcc.gnu.org/)...), then improve your code to get no warnings, then **use the debugger** (`gdb`) and repeat till you are happy with your code. – Basile Starynkevitch Mar 07 '17 at 06:38
  • 1
    @StoryTeller That's true, but allowing the length modifier there does not make that a requirement, does it? It is perfectly valid (and I'd say, expected) to use `%f` for `doubles`. Well, APIs got their requirements, inn'it? :) – Sourav Ghosh Mar 07 '17 at 06:41
  • @SouravGhosh - I'd disagree. All other format specifiers for printf and scanf match. This mismatch is poor design. It's "allowed" because it cannot be "required' (argument promotion and all that). But it most certainly should be required in code review. – StoryTeller - Unslander Monica Mar 07 '17 at 06:43
  • 1
    BTW, you misspelt "[Celsius](https://en.wikipedia.org/wiki/Celsius)". – Toby Speight Mar 07 '17 at 10:43

1 Answers1

4

You got wrong conversion specifier there with double for scanf(). You need to use

  scanf("%lf", &C);

To add the relevant quote, from C11, chapter §7.21.6.2

l (ell)

Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long int or unsigned long int; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double; or that a following c, s, or [ conversion specifier applies to an argument with type pointer to wchar_t.

That said, you must check the return value of scanf() to ensure the success of the call. Otherwise, you'll end up using the value of an uninitialized variable.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234