-4

I am a university student. When learning C language, I encountered the problem of floating point division. Can anyone help me to see why the results are different every time I run it. thank you very much.( I am running in a Linux)

My English is not good, hope you can understand the meaning of my question。

#include <stdio.h>
int main()
{
    double a = 1;
    double b = 2;
    printf("%d\n",b/a);
    return 0;
}
  • 2
    What exactly is "the problem of dividing floating-point numbers"? – Scott Hunter Sep 23 '22 at 13:42
  • Please post code as text, not an image of text. – Scott Hunter Sep 23 '22 at 13:42
  • 2
    Welcome to SO. You should listen to your compiler warnings. It should tell you about parameter type mismatch in `printf`. You promise to provide an `int` value but instead you pass a `double`. This is causing undefined behaviour which can include any kind of strange result. If you did not get a warning, turn up warning level. For GCC use `-Wall -Wextra -pedantic` – Gerhardh Sep 23 '22 at 13:44
  • 1
    [Don't post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question). Copy-paste text, especially code, *as text* into your questions. – Some programmer dude Sep 23 '22 at 13:44
  • 1
    `printf( "%lf\n", b / a );`. Specifier `%d` is for `int`. – i486 Sep 23 '22 at 13:45
  • 1
    Also please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 23 '22 at 13:45
  • 5
    [Picture of my comment](https://i.stack.imgur.com/weDgp.png) – Lundin Sep 23 '22 at 13:45
  • Sorry, this is my first time posting a question, thank you for reminding me. – laihonglin Sep 23 '22 at 13:55
  • laihonglin, Save time. Enable **all** compiler warnings to quickly see the problem with `printf("%d\n",b/a);`. – chux - Reinstate Monica Sep 23 '22 at 14:07

1 Answers1

0

You're using the wrong format specifier.

The %d former specifier for printf expects an int as an argument, but you're passing in a double. Using the wrong format specifier triggers undefined behavior.

You should instead use %f which is for printing a double.

printf("%f\n",b/a);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Why does this happen if it is %d – laihonglin Sep 23 '22 at 13:59
  • 1
    @laihonglin Mismatching format specifier and argument type leads to *undefined behavior*. – Some programmer dude Sep 23 '22 at 14:01
  • @laihonglin Different types can have different sizes and can be passed to a function in different ways. And because all arguments to `printf` after the first don't have a specific type, those details become important. – dbush Sep 23 '22 at 14:01
  • The compiler does not report an error, and the result is always 0 in windows – laihonglin Sep 23 '22 at 14:03
  • 1
    @laihonglin That's part of undefined behavior. There's no expectation of any particular result. – dbush Sep 23 '22 at 14:05
  • @Some programmer dude This kind of code is an error code, and this kind of code is not allowed in C language, right? – laihonglin Sep 23 '22 at 14:06
  • 1
    @laihonglin That's right, it's not allowed. Most compilers can actually detect it and emit a warning about it. If it doesn't for you, then you need to enable more warnings when building. With MSVC add the `/W4` build option, with GCC and Clang use `-Wall -Wextra`. – Some programmer dude Sep 23 '22 at 14:08