5

I have declared the function pow in the traditional way of declaration in C. However, the output of function is incorrect. I don't need to include math.h here as I am declaring the function and the object code for it is already present. Here is my code:

#include<stdio.h>

double pow();           //traditional declaration doesnt work, why??

int main()
{
    printf("pow(2, 3) = %g", pow(2, 3));
    return 0;
}

The output for the above is 1 whereas it should be 8. Please help me with it.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Yatn Bangad
  • 53
  • 1
  • 3
  • Hello close voter for "no MCVE". Please explain what is missing. I can see one right there in the question and it is even explained in all its weird detail. – Yunnosch Oct 03 '19 at 05:46

1 Answers1

5

traditional declaration doesnt work, why??

Because without a prototype, those two ints you provided don't get converted to the doubles that pow actually accepts. With "traditional" declarations you must painstakingly make sure you provided exactly the type of arguments expected by the function, or you'll have yourself undefined behavior in your program.

This is one reason to favor prototypes, and to actually use the standard library headers that provide them for standard functions.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458