7

I am trying to learn C and have come up with the following small program.

#include "stdafx.h"

void main()
{
    double height = 0;
    double weight = 0;
    double bmi = 0;

    printf("Please enter your height in metres\n");
    scanf_s("%f", &height);
    printf("\nPlease enter your weight in kilograms\n");
    scanf_s("%f", &weight);
    bmi = weight/(height * height);
    printf("\nYour Body Mass Index stands at %f\n", bmi);
    printf("\n\n");
    printf("Thank you for using this small program.  Press any key to exit");
    getchar();
    getchar();
}

The program compiles perfectly, however the answer returned by the program does not make sense. If I enter 1.8 for height and 80 for weight, the bmi is like 1.#NF00 which does not make sense.

What am I doing wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Matthew
  • 4,477
  • 21
  • 70
  • 93
  • 10
    Isn't it `%lf` for doubles? In any case, [you shouldn't use `void main`.](http://stroustrup.com/bs_faq2.html#void-main) – chris Jul 26 '12 at 14:42
  • @chris good thing to know. I was always taught that `void main` was for C, and `int main` was for C++. I wonder where the habit for new C/C++ programmers to do this comes from. – Casey Kuball Jul 26 '12 at 18:53
  • @Darthfett, I think there are a few books that use `void main`. I would imagine that's where all of this is being passed on from. – chris Jul 26 '12 at 18:58

6 Answers6

11

When using scanf with a double, you must use the %lf specifier, as pointers are not promoted with scanf.

For more info, read the following question: Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Community
  • 1
  • 1
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
10

scanf (and scanf_s) format %f expects pointer to type float.

Simply change the type of your height and weight variables to float to fix this.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
4

I think issue in scanf_s syntaxis, you ommited 3-rd argument, which is size of buffer in bytes. Try following:

scanf_s("%lf", &valueToGet, sizeof(double));
spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • 1
    Uhm, he's not scanning a `char` here, you know – Richard J. Ross III Jul 26 '12 at 14:46
  • yes, i know this I just example to show what 3-rd argument is needed, which I took quickly from msdn, as I tried to respond quickly on answer I not adjust that example on given task, sorry for that )) – spin_eight Jul 26 '12 at 14:49
  • Presumably you are talking about `scanf_s`, but you've written `scanf` ? Also the format specifier is wrong. Please either fix the mistakes or delete the answer. – Paul R Jul 26 '12 at 14:51
  • 2
    Well done - down-vote removed - note however that the `8` should really be `sizeof(double)`. – Paul R Jul 26 '12 at 16:13
  • Yes, I just enter to correct that. Surely to make code more portable instead of hardcoded numbers better to use expressions like sizeof(type) – spin_eight Jul 27 '12 at 07:49
  • 1
    `sizeof valueToGet` would be more robust. – wildplasser Jul 27 '12 at 07:52
3

the drawback of the scanf() and printf() is that it requires very strict format, any mismatch between the control string and the argument can cause drastic error which makes your input or output make no sense at all. And that mistake is often made by beginners.

duleshi
  • 1,966
  • 2
  • 21
  • 32
2

If you're using %f format specifier, then you must use float data type instead of double.

Fahad Naeem
  • 515
  • 7
  • 15
0

The problem is because:

format '%f' expects argument of type 'float*', but argument 2 has type 'double*' 

There are two ways to handle this:

  1. Either the variables should be float:

    double height = 0;    -->    float height = 0;
    double weight = 0;    -->    float weight = 0;
    double bmi = 0;       -->    float bmi = 0;
    
  2. or the format specifier should correspond to double.

    scanf_s("%f", &height);   -->    scanf_s("%lf", &height);
    
    scanf_s("%f", &weight);   -->    scanf_s("%lf", &weight);
    
    printf("\nYour Body Mass Index stands at %f\n", bmi);
                                              |
                                              V 
    printf("\nYour Body Mass Index stands at %lf\n", bmi);
    
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43