Thread: Another question from the internet cafe

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    Another question (manual prototypes?)

    I can´t believe this, but I just found out that the internet cafe has a copy of vc++6 on it! wow! (not joking)

    so anyways, i have another question.

    headers include function prototypes for functions in the standard library, correct?

    why doesn´t this program work then?

    Code:
    double sin(double);
    
    int main()
    {
    	sin(5.0);
    }
    i suppose i can´t prototype the function myself... but why?
    Last edited by krygen; 12-24-2004 at 02:08 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    yes headers do contain prototypes, but they also 'import' the function definition with the keyword "extern" which grabs the functions out of the local library
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > why doesn´t this program work then?
    How doesn't it work?
    To me, all you are doing is calculating the value of sin(5)
    You throw the result away!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ignoring the fact that using a standard name for your own identifier is either a gross error or bad form, what were you expecting it to do? Magically determine that the prorotype you gave for sin actually meant the standard sin and to link with the math library so that everything works like you wanted? C++ isn't quite as lenient as C in this area.
    My best code is written with the delete key.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but they also 'import' the function definition with the keyword
    >"extern" which grabs the functions out of the local library
    Oh, so adding extern will work? And here I thought extern was the default linkage for functions in global scope, so a function declared with extern and a function declared without extern were equivalent. I get the feeling you meant something other than what you said.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Prelude
    I get the feeling you meant something other than what you said.
    no, i meant what i said...at the time....
    i've been looking at header files since then and realized i was most likely mistaken..

    this type of thing is something i've never paid close attention to nor had to implement .
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Compile these two programs. Be sure to use the compiler setting that gives you a preprocessed file as well:
    Code:
    double sin(double);
    
    int main()
    {
      sin(5.0);
    }
    Code:
    #include <cmath>
    
    int main()
    {
      sin(5.0);
    }
    You may be surprised at what you find. There's more to a standard header than just declarations.
    My best code is written with the delete key.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by krygen
    why doesn´t this program work then?
    This works on my compiler, but it is not the recommended way of doing things. The following code is bad (and so on).
    Code:
    extern "C" 
    {
        double sin(double); //Prototype to the C library
    }
    
    int main()
    {
        sin(5);
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    I supposed I figured that linking with the standard library is the same as linking to another file. e.g when I create my own function in another file and link it, all i need to do is prototype it in the first file. But the standard library is different?

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I supposed I figured that linking with the standard library is the same as linking to another file. e.g when I create my own function in another file and link it, all i need to do is prototype it in the first file. But the standard library is different?
    No, they're very similar. No matter what, if you want to use a function, somewhere it has to be defined. Code doesn't just come out of nowhere. When you create your own library's to reuse, you prototype it in a header file, but that header file is linked to the library, in which the functions are then defined. The standard library works the same way. You include a header file, in which there are function prototypes, macros and the like, but that header file is linked to a library in which all those functions are defined. The library becomes part of your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 internet connections
    By Erakis in forum Windows Programming
    Replies: 0
    Last Post: 07-01-2009, 07:14 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question about internet hosting
    By procyon4476 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-03-2004, 07:35 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM