-4

My code is:

#include<stdio.h>
#include<math.h>

int main()
{
    int c = pow(3,2);

    printf("%f   %f   %d \n\n",pow(3,2),c,c);
    printf("%f   %d   %f \n\n",pow(3,2),c,c);

    return 0;
}

The output is:

9.000000   0.000000   4200688

9.000000   9   0.000000

Can anyone explain why in the first line of output instead of printing 9 (like it does in the 2nd line of output) it prints 4200688 (garbage value probably)?

  • 3
    Use correct format specifier. period – Sourav Ghosh Jun 11 '18 at 08:21
  • I have purposefully used %f as one of the format specifiers to know how exactly is this changing the output drastically in the 2 lines just by mere change in position. – patrick1024 Jun 11 '18 at 08:23
  • Then your purpose is wrong. – Sourav Ghosh Jun 11 '18 at 08:24
  • My purpose is to understand how is changing position of format specifiers give inconsistent results. Shouldn't 2nd output also be 9.0000 garbage value 0.000000 – patrick1024 Jun 11 '18 at 08:30
  • 1
    Please, seek informations on the concept of "undefined behavior" in the C programming language. [This](http://en.cppreference.com/w/c/language/behavior) might be a good starting point. – Bob__ Jun 11 '18 at 08:32

1 Answers1

1

The code you mentioned having undefined behaviour. Read the manual page of pow function and check it's prototype. It says

double pow(double x, double y);

In this statement

int c = pow(3,2);

And use correct format specifier and don't play with compiler warning while compiling with -Wall flag, listen to warnings.

warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Werror=format=]
          printf("%f   %f   %d \n\n",pow(3,2),c,c);
warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Werror=format=]
     printf("%f   %d   %f \n\n",pow(3,2),c,c);

From the C99 standard section 7.19.6.1

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • Basically, what i infer from this discussion is that if any of my format specifier is incorrect in a line of code, then the output of my program for that piece of code can never be predicted and it is completely random. – patrick1024 Jun 11 '18 at 08:47
  • 1
    _then the output of my program for that piece of code can never be predicted and it is completely random ?_ C standard defines that as _undefined behavior_. And practically you shouldn't write anything in your code which causes UB. – Achal Jun 11 '18 at 08:48
  • @CSEnthusiast "output of my program for that piece of code can never be predicted" --> C does not specify the output is _unpredictable_. The behavior is _undefined_. "output of my program for that piece of code ... is completely random" --> C does not specify the output is _random_. The behavior is _undefined_. The point being, the result is _undefined_, not defined to be something unusual. – chux - Reinstate Monica Jun 11 '18 at 17:37