exponential question

This is a discussion on exponential question within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> main () int a=2;int b=5; int c; c = pow(a,b); printf("&#37;d to the power of ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    5

    exponential question

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main ()
    
    int a=2;int b=5; int c; c = pow(a,b);
    
    printf("&#37;d to the power of %d is equal to %d\n",a,b,c);
    
    }
    this program called ma.c is simply trying to output: 2 raise to
    the power of 5, but i get this compilation error below, can anyone tell me what did i missed here, im new to c programming.


    bt ~ $ gcc -o ma ma.c
    /tmp/cck7y346.o(.text+0x3f): In function `main':
    : undefined reference to `pow'
    collect2: ld returned 1 exit status
    Last edited by Salem; 11-09-2007 at 03:10 AM. Reason: Added code tags AROUND the code, not just inserted to make the popup go away.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Add "-lm" to include the "libm" library to your build - it is not included by default, because most systems don't use the math library - so if you DO need it, you have to ask for it "by hand".

    Also, if you want 2 ^^ 5, you can do that by "1 << 5", rather than calling pow() which converts arguments to floating point, does expensive exponential functions - it's probably 30-100 times slower than the 1 << 5 method [which is one or two clock-cycles on most processors].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    5

    thanks

    thanks mat, it works fine


    bt ~ $ gcc -lm -o ma ma.c
    bt ~ $ ./ma
    2 to the power of 5 is equal to 32

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Next time, add [code][/code] tags around the code.
    Don't rip out all the braces to get rid of the pop up just so you can post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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