openlang

Why does replacing float with double make this C program always print zero although the float version gives the right output?

With float my program looks like this:

#include <stdio.h>

int main() {
    float result;
    float input;
    scanf("%f", &input);
    result = input / .25;
    printf("%f", result);
    return 0;
}

Entering the value 3 produces 12.000 as output. Replacing float with double, however, consistently gives me 0.000 instead. Is there something I'm overlooking here?

The conversation continues 2

Follow the reasoning
  1. Permalink

    As explained in this question about scanning doubles (https://stackoverflow.com/questions/33269283/scanf-not-reading-in-double),

    Your claim to the compiler was false: for scanning, %f promises a pointer to float. What you actually supplied was a pointer to double.

    Either switch to %lf or make the input a float.

    Note that printf has different format rules, accepting %f with either float or double arguments. This works because the arguments undergo promotion to doubles instead of being pointers.

Other sides of the same question

  1. How can I subclass a class inside another class?

    Hi, In a package whose code is outside my control, there is a hierarchy of classes arranged as follows: I'm trying to add a w…

    Replies 1Latest activity scala
  2. Comparing have with let bindings in Lean4

    I've observed that these two expressions and behave identically. This also happens in theorem proofs. Are they interchangeabl…

    Replies 3Latest activity lean-4
  3. TypeScript Map initialization with string array entries

    I'm creating a Map whose entries each hold an array of strings. How can I declare its type and initialize it together in one…

    Replies 2Latest activity typescript