Header-files

This is a discussion on Header-files within the C Programming forums, part of the General Programming Boards category; Hi All And Sundry, I want to have my C declarations only in a header file and include it in ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Header-files


    Hi All And Sundry,
    I want to have my C declarations only in a header file and include it in my program. I believe this has gotta do with the concept of pre-compiled headers but am very fuzzy .
    Could you please help< It would be cool if specially u could tell me how to do it in Turbo C/C++ and not gcc but even if not id be grateful>.

    Thx a lot for ur time.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    This is what your file looks like before
    Code:
    // prog.c
    #include <stdio.h>
    
    int foo ( );
    int bar ( char *msg );
    
    int main ( ) {
        int a = foo();
        int b = bar( "hello " );
        printf( "%d %d\n", a, b );
        return 0;
    }
    
    int foo ( ) {
        return 0;
    }
    int bar ( char *msg ) {
        printf( "%s", msg );
        return 0;
    }

    And this is what it looks like when you've created funcs.c and funcs.h

    Code:
    // funcs.h
    int foo ( );
    int bar ( char *msg );
    
    // prog.c
    #include <stdio.h>
    #include "funcs.h"
    int main ( ) {
        int a = foo();
        int b = bar( "hello " );
        printf( "%d %d\n", a, b );
        return 0;
    }
    
    // funcs.c
    #include <stdio.h>
    #include "funcs.h"
    int foo ( ) {
        return 0;
    }
    int bar ( char *msg ) {
        printf( "%s", msg );
        return 0;
    }
    You would compile this using say
    bcc proc.c funcs.c
    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.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I do this with all my prototypes.
    List them under the program file they originate from.
    Then include the header in all files.

    I use a conditional compilation to ensure that the files can be compiled individually but will only be included once in the build.

    #ifndef APP_PROTOTYPES
    #define APP_PROTOTYPES

    int MyFunction(HWND,int,char *);

    #endif

    note that I do not include the name of the variable in the prototype.
    I use MS DEVStudio but should work in other compliers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > note that I do not include the name of the variable in the prototype.
    But there's no good reason not to include the variable names in the prototypes.

    It does help to document the function

    void copy_string ( char*, char* );

    or

    void copy_string ( char *to, char *from );
    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.

  5. #5
    mukherjeeanupam
    Guest

    Thx

    Thanks a lot for taking the time to help me out .That was exactly what i wanted to know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 06:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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