0

So basically, I need to get an input and square it. some reason the pow() function isn't working.

#include <stdio.h>
#include <math.h>
int main (){

  int in, ans;
  scans("%d", &in);
  ans = pow(in, 2);
  printf("answer is: %d", ans);
  return 0;

}

The error is :

undefined pow or something like that

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • 8
    answer is , 'show exact error message' , or something like that – pm100 Mar 27 '17 at 17:11
  • Assuming a linker error? Hard to say when we don't have an exact error message. Could always do `in * in` to square it too. – Michael Dorgan Mar 27 '17 at 17:13
  • I strongly suggest squaring the input by multiplying it by itself, especially since it is an integer, and the answer is (apparently) expected to fit in an integer. `ans = in * in;` – John Bollinger Mar 27 '17 at 17:15
  • 4
    Carefully reproducing error message is a **key prerequisite** to getting good answers. You wouldn't be satisfied with an answer like "there must be a library missing or something like that" would you? – fvu Mar 27 '17 at 17:16
  • 1
    Possible duplicate of [Why do you have to link the math library in C?](http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c) – ad absurdum Mar 27 '17 at 17:50

3 Answers3

2

This type of error occur when you forgot to link your programme to the math library .
use -lm for linking to math library.

gcc yourfilename.c -lm

to use the math library

Ankur Jyoti Phukan
  • 785
  • 3
  • 12
  • 20
0

I think you have compiled on Linux platform. So, use -lm option with command.

gcc yourfile.c -lm
msc
  • 33,420
  • 29
  • 119
  • 214
0

You are using scans instead of scanf. It should scanf("%d",&in);

Mohit Yadav
  • 471
  • 8
  • 17