Thread: C Newbie problems with functions (I think)

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15

    C Newbie problems with functions (I think)

    Why?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       float x;
    
       float round(float number);
    
       while(1)
          {
             printf("Enter a number");
             scanf("%f", &x);
             printf("round(%f) = %f", x, round(x));
          }
    
       return 0;
    }
    
    
    
    float round(float number)
    {
       return floor(number +.5);
    }
    gcc ...

    Code:
    05_10.c: In function ‘main’:
    05_10.c:8: warning: conflicting types for built-in function ‘round’
    /tmp/ccgnH0xg.o: In function `round':
    05_10.c:(.text+0x6b): undefined reference to `floor'
    collect2: ld returned 1 exit status

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a standard function in math.h that takes a double and returns a double, called round. This is what the compiler is moaning about.

    man round

    If you want to use the functions in math.h, you need to link to libm, so you need -lmon the gcc line.

    --
    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
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15
    mmm, maybe I didn't understand something ... this's my new code

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       double x;
    
       double myround(double number);
    
       while(1)
          {
             printf("Enter a number");
             scanf("%f", &x);
             printf("myround(%f) = %f", x, myround(x));
          }
    
       return 0;
    }
    
    
    
    double myround(double number)
    {
       return floor(number +.5);
    }
    Did you mean gcc -lmon asd.c?

    Code:
    $: gcc -lmon asd.c 
    /usr/bin/ld: cannot find -lmon
    collect2: ld returned 1 exit status
    I use Debian Gnu/linux and Glibc,

    Code:
    $: ls /usr/include/|grep ^math
    math.h

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, missed a space in the original post: the command should be something like:
    Code:
    gcc -lm blah.c -o blah
    --
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15
    Ok, it works!

    thank you again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob + Functions == Problems
    By Inao in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2007, 04:02 PM
  2. Im a newbie having problems with dynamic arrays, can anyone help?
    By Dr.Spankenstein in forum C Programming
    Replies: 4
    Last Post: 04-16-2007, 04:54 AM
  3. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  4. Newbie asking about overloading functions
    By GLR in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 10:54 AM
  5. problems with functions
    By catalyst in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 06:55 AM