0

I am trying to learn some C and I have an issue I can't figure out. Seems pretty simple but the below is not working.

#include <stdio.h>
#include <stdlib.h>

int main() {

    printf("%f", pow(4, 3) );
    return 0;
}

Error:

Implicitly declaring library function 'pow' with type 'double (double, double)'

I have the string formatter set to float so I am not sure why I am seeing that error

JD2775
  • 3,658
  • 7
  • 30
  • 52
  • 9
    `#include ` – Eugene Sh. Mar 19 '21 at 17:01
  • 1
    What header contains the function prototype for the pow function? – nicomp Mar 19 '21 at 17:01
  • 1
    That's not an error, it's a warning. You can still run the executable – Dock Mar 19 '21 at 17:02
  • @EugeneSh. Thank you. The tutorial I am watching does not have that in the code, I dont know how theirs is even running. It matches mine exactly. Adding that fixed it. – JD2775 Mar 19 '21 at 17:04
  • 3
    Be very careful with C tutorials, many of them are really not worth the time. Always have a [high-quality reference book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) on hand to validate anything you're being taught. – tadman Mar 19 '21 at 17:04
  • 1
    Tutorials are not perfect. Some are really bad. – Gerhardh Mar 19 '21 at 17:04
  • 2
    @Dock, whether it's a warning or error is configurable in most compilers. Many of us would treat that as an error. – jwdonahue Mar 19 '21 at 17:06
  • I have a "C - Primer Plus" book I bought years ago but never really looked at. Maybe I would be better off working with that :). Thanks everyone – JD2775 Mar 19 '21 at 17:07
  • @Dock. Without prototype the compiler will assume `int` type return value and only pass an `int` to `printf` which might not be a syntax error but will still result in garbage. – Gerhardh Mar 19 '21 at 17:07

2 Answers2

0

The pow() is in math.h header file.
The format specifier for double should be used.

https://www.programiz.com/c-programming/library-function/math.h/pow

Obsidian
  • 3,719
  • 8
  • 17
  • 30
Reynadess
  • 1
  • 3
  • What's wrong with the format specifier? `printf` expects a `double` argument for `%f`. – Gerhardh Mar 19 '21 at 17:05
  • 2
    I mistakenly commented (deleted) that `%lf` should be used, but that is for `scanf()`. The `%f` with `printf()` is good, and `float` is promoted to `double`. – Weather Vane Mar 19 '21 at 17:06
0

include library #include <math.h> and change printf("%f", pow(4, 3) ) line to printf("%lf", pow(4, 3) ) , because pow() returns double

Kanony
  • 509
  • 2
  • 12