Thread: pow in non main function.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    pow in non main function.

    i cant get the pow() function in math.h to work in a function other than main. heres my code
    Code:
     #include <stdio.h>                                                    
    #include <math.h>
    
    int bintoint(char * bin);
    int main()
    {
        printf("%lf", pow(2, 8));
        
        return 0;
    }
    
    int bintoint(char * bin)
    {    
        int count = 0, total = 0, temp = 1;
        while (bin[count] != '\0')
        {
            count++;
        }
        
        while(count-- != 0)
        {
            printf("%lf", pow(2, count));
        }    
        return total;
    }
    (note its not complete)
    can you tell me how to fix this?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    In C89 (Microsoft's Visual Studio is a C89 compiler), use "%f" to print a value of type double; use "%Lf" to print a value of type long double (note difference with scanf()).
    The function pow() returns a double.

    Change the printf format string.

    Using "%lf" in a printf format string, in C89, is Undefined Behaviour.
    Last edited by qny; 11-21-2012 at 12:19 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,176
    [edit] answered by qny

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    468
    Could the problem be that you don't ever call bintoint from main?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,326
    I don't see how this is specific to Microsoft's Compilers. To printf a double, use %f, and to scanf a double, use %lf. If you're dealing with long doubles, however, one might have to pay attention to the compiler and/or C runtime which is being used.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Many compilers today compile C99.
    In C99, using "%lf" in printf with a corresponding double value is accepted ... (7.19.6.1/7 l (ell) ... has no effect on a following ... f ... conversion specifier).

    So the original code behaved differently (UB vs defined behaviour) when compiled/run in different implementations.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    error message

    gcc -Wall -o "1" "1.c" -std=c99 (in directory: /home/sandbucket/C/chapter 15)
    1.c: In function ‘bintoint’:
    1.c:14:28: warning: unused variable ‘temp’ [-Wunused-variable]
    /tmp/cczXWO9g.o: In function `bintoint':
    1.c.text+0x95): undefined reference to `pow'
    collect2: error: ld returned 1 exit status
    Compilation failed.
    this is the error message i get.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You need to include the math library proper in the linking stage. The header is not enough.
    Add the option -lm to your compiler invocation

    Code:
    gcc -Wall -o 1 1.c -std=c99 -lm
    You don't get the error in main because the compiler itself is doing the calculation (*), just as if you had writen
    Code:
        printf("%lf", 256);
    (*) gcc (4.3) uses MPFR
    Last edited by qny; 11-21-2012 at 03:48 PM.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    ohh true macros thanks alot bud

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    reply

    Quote Originally Posted by sandbucket View Post
    ohh true macros thanks alot bud
    idk if that is 100% right. i i dont want to sully my name on this forum right once i first join.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by sandbucket View Post
    idk if that is 100% right
    Almost 100%
    It's no more a macro than the compiler calculating some multiplications ...

    Code:
    timestamp = 6549864651;
    days = timestamp / (24*60*60); // compiler emits code to divide by 86400

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    468
    Quote Originally Posted by qny View Post
    Almost 100%
    It's no more a macro than the compiler calculating some multiplications ...

    Code:
    timestamp = 6549864651;
    days = timestamp / (24*60*60); // compiler emits code to divide by 86400
    And at a better optimization level the compiler might emit code to set days to 75808, without a division.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  2. Replies: 2
    Last Post: 09-19-2012, 11:32 AM
  3. Replies: 35
    Last Post: 12-01-2011, 07:31 PM
  4. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  5. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 12:31 PM

Tags for this Thread