0
#include <stdio.h>

float diff_abs(float,float);

int main() {
  float x;
  float y;
  scanf("%f", &x);
  scanf("%f", &y);
  printf("%f\n", diff_abs(x,y));
  return 0;
}

float diff_abs(float a, float b) {
  float *pa = &a;
  float *pb = &b;
  float tmp = a;
  a = a-b;
  b = *pb-tmp;
  printf("%.2f\n", a);
  printf("%.2f\n", b);
}

Hello guys, i'm doing a C program which should keep in a variable a-b, and in b variable b-a. It's all ok, but if i run my code at the end of output, compiler shows me this message:

3.14
-2.71
5.85
-5.85
1.#QNAN0

what does means 1.#QNANO?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Fulvio Denza
  • 67
  • 1
  • 2
  • 11
  • p.s. 3.14 and -2.71 is the input, 5.85 and -5.85 are input and output – Fulvio Denza Nov 24 '16 at 14:57
  • 1
    Nothing of that outout is from the compiler. And your code invokes undefined behaviour. Why do you igonre compiler warnings? The code also shows other missconceptions. C is strictly pass-by-value! – too honest for this site Nov 24 '16 at 14:57
  • 1
    You don't have a return in the function. – Jonathan Leffler Nov 24 '16 at 14:58
  • Did you google for `1.#QNAN` first? [1.#QNAN error C++](http://stackoverflow.com/q/4617796/995714), and it's `QNAN0`, not `QNANO` [1.#QNAN0 output when calculating standard deviation C](http://stackoverflow.com/q/29210975/995714) – phuclv Nov 24 '16 at 14:59
  • I reopened the question as it was marked to a [wrong dupe](http://stackoverflow.com/questions/8276434/could-someone-explain-what-does-1-qnan-mean). – Sourav Ghosh Nov 24 '16 at 15:03
  • Yeah, i know, today i began to study pointers and vectors, so i'm new in this type of programming – Fulvio Denza Nov 24 '16 at 15:04
  • @LưuVĩnhPhúc While the links are all useful, in this particular case, the UB is the reason behind the output and hence, cannot be justified anyway. :) Just sayin'. – Sourav Ghosh Nov 24 '16 at 15:06
  • Possible duplicate of [If a function returns no value, with a valid return type, is it okay to for the compiler to throw garbage?](http://stackoverflow.com/questions/9936011/if-a-function-returns-no-value-with-a-valid-return-type-is-it-okay-to-for-the) – P.P Nov 24 '16 at 15:08
  • [Could someone explain what does “-1.#QNAN” mean?](http://stackoverflow.com/q/8276434/995714) – phuclv Nov 24 '16 at 15:32
  • @SouravGhosh yes I overlooked the last question and forgot about the code – phuclv Nov 24 '16 at 15:33

2 Answers2

1

The problem is, you don't return a value from the called function diff_abs() and you're trying to use the return-ed value. It invokes undefined behavior.

Quoting C11, chapter §6.9.1, Function definitions

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

Based on your comments, it appears, you are not needed to have any return value from the function. In that case,

  • Change the function signature to return type void
  • Just call the function, get rid of the last printf() call in main() altogether. The printf()s inside the called function will get executed and the prints will appear on their own, you don't need to pass the function as an argument to another printf() for that.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • i tried lot of way, for example i put at the end of the function a return 0, return a && b, and so on, but anyway the terminal don't show me just 5.85 and -5.85 – Fulvio Denza Nov 24 '16 at 15:17
  • @FulvioDenza change `printf("%f\n", diff_abs(x,y));` to `diff_abs(x,y);`, i guess that's what you need. – Sourav Ghosh Nov 24 '16 at 15:20
1

In this piece of code printf("%f\n", diff_abs(x,y)) you are telling the compiler to print a float type of variable which should be the return value of the function diff_abs. But in your function diff_abs you are not returning any value.

So %f which is waiting for a float, will not get any value and it will print #QNAN0 which means Not A Number. So you can change your code as follows:

In your main:

//printf("%f\n", diff_abs(x,y));   //comment this line
diff_abs(x,y);  //just call the function

In the function:

void diff_abs(float a, float b) {  //change the return value to void
  //float *pa = &a;   //you are not using this variable
  float *pb = &b;
  float tmp = a;
  a = a-b;
  b = *pb-tmp;
  printf("%.2f\n", a);
  printf("%.2f\n", b);
  return;
}
ReshaD
  • 936
  • 2
  • 18
  • 30