POW() func giving weird output

This is a discussion on POW() func giving weird output within the C Programming forums, part of the General Programming Boards category; Hi everyone, I ran into problems with the pow() function in my main program so I isolated the problem in ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    POW() func giving weird output

    Hi everyone,

    I ran into problems with the pow() function in my main program so I isolated the problem in another c file to test out but I am still not sure what could be wrong.

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char **argv) {
      int i = atoi(*(++argv));
      int d = atoi(*(++argv));
      printf("%d^%d=%d\n",i,d,pow(i,d));
      // the output of this is i^d=i, not sure why
    }
    As noted in the comments but the pow() function is not working correctly and I am already using the -lm option because I am using gcc.

    I get the above output of i^d=i for any combination of i and d. Any idea what could be wrong?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    pow function returns a double or other non-int types pow - C++ Reference
    In your printf you use %d to print the result while you should use %f for a double.

    PS-run your code after removing the malloc.h (gcc won't recognize it)

    PS2- it is not only because gcc.Maybe you are on linux.I used gcc and did not have to use it

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Ah ok. I thought this might be the cause of the problem in my other program but I guess it probably isn't. Thanks for the help. The other includes were for my other program which is why they were here.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    Quote Originally Posted by workisnotfun View Post
    Ah ok. I thought this might be the cause of the problem in my other program but I guess it probably isn't. Thanks for the help. The other includes were for my other program which is why they were here.
    Glad that helped Good luck with the project~!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code giving undesired output..
    By somali.cc in forum C Programming
    Replies: 7
    Last Post: 10-06-2012, 11:38 AM
  2. Why is this giving me this output?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-16-2012, 12:51 PM
  3. Replies: 4
    Last Post: 12-23-2011, 07:56 AM
  4. structure giving weird output
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-14-2010, 10:44 PM
  5. sprintf being weird and giving me a segfult.
    By redruby147 in forum C Programming
    Replies: 10
    Last Post: 02-07-2010, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21