prototypes and multiple modules

This is a discussion on prototypes and multiple modules within the C Programming forums, part of the General Programming Boards category; if i have 2 source code files, one has function definitions and the other has the code, do the functions ...

  1. #1
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38

    prototypes and multiple modules

    if i have 2 source code files, one has function definitions and the other has the code, do the functions need to be prototyped only in the module that uses the function? it compiles fine if i prototype them in both files but i want to know the standard.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Function definitions go in the .h file, and then the actual code (no prototypes) goes in the .c file.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > it compiles fine if i prototype them in both files but i want to know the standard.
    You mean the prototypes are in a header file which you include in both files.

    Yes, this is the best way to do it.

    For a start, if you change the function without changing the header, you'll start to get very important warnings.
    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.

  4. #4
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    thats not what c for dummies chapter 14 is teaching me.

    right now in file1.c i have the function prototype and the function usage, and in file2.c i have the function definition.

    what youre saying is in file1.c i should have just the function being used, in file2.c i should have the function definition with an #include "header.h", and in header.h i should have the prototype for that function?
    Last edited by Bigbio2002; 11-19-2003 at 02:15 PM.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bigbio2002
    thats not what c for dummies chapter 14 is teaching me.

    right now in file1.c i have the function prototype and the function usage, and in file2.c i have the function definition.

    what youre saying is in file1.c i should have just the function being used, in file2.c i should have the function definition with an #include "header.h", and in header.h i should have the prototype for that function?
    Yeah. Something like this:
    Code:
    /* header.h */
    #ifndef HEADER_H
    #define HEADER_H
    
    void myfunction( void ); /* prototype */
    
    #endif
    /* header.h ends */
    
    /* file1.c */
    #include "header.h"
    
    int main( void )
    {
        myfunction( );
    
        return 0;
    }
    /* file1.c ends */
    
    /* file2.c */
    #include "header.h"
    
    void myfunction( void )
    {
        ...do something...
    }
    /* file2.c ends */
    Now you can compile and link these and it should work just fine. You can use 'myfunction' in file1.c because it is prototyped (by means of the #include statement), so this file knows the general idea of how the function works. (Its prototype; it knows what arguments it takes so it can handle it correctly.)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    alot of files to get 1 little thing to work. im trying to make my own module that i can use for other programs i created. it seems useless to create 2 files just for 1 thing. any suggestions?
    Last edited by Bigbio2002; 11-19-2003 at 02:35 PM.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to create two files for your module:
    1) The header file prototypes all of the functions you want available for other files. The "interface" as it were.

    2) The .c file contains all variable and actual functions themselves. Once this file is finished and compiled, you never have to do anything to it again.

    To use your module, all you do then is #include your header file in whatever file you want to use it.

    Take my previous example. It illustrates it clearly. The file2.c would be your module. This has all the functions and what not that your module uses. The header is included in whatever file you need to access/use your module from.

    Looking again at my example, you'll see that in order for file1.c to be able to use the function in your module, it needed to include the header file for said module.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 08:31 AM
  2. Multiple Modules
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 04:34 PM
  3. Multiple Files and Struct
    By gsoft in forum C Programming
    Replies: 4
    Last Post: 01-12-2005, 04:44 AM
  4. multiple header #includes
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-30-2002, 10:04 PM
  5. globals & multiple .c files
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-30-2002, 09:43 PM

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