.h file vs .c file

This is a discussion on .h file vs .c file within the C Programming forums, part of the General Programming Boards category; Hi all, Just a quick question. My one .c file programs are starting to get very large. To break this ...

  1. #1
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28

    .h file vs .c file

    Hi all,

    Just a quick question. My one .c file programs are starting to get very large. To break this down a bit, I can either create more .c files in the same project or put some code in .h files. I take it, if the code is going to be changed, put it in .c files, but if code is constant and never changed, put in a .h file. Is this correct?

    Look forward to your reply.

    RocketMan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,606
    No. ".h" is a typical file extension for C header files. ".c" is a typical file extension for C source files.

    Therefore, put your implementation code in the source files, and also provide the appropriate header files with the function, struct and other declarations that the source files will need.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Sorry I will re-word that. As an example if I have functions - 1ms, 10ms, 1sec, 1min etc. Do I make a header file delay.h or make another "c" file delay.c? A quick example is below:

    Code:
    #include<stdio.h>
    #include<delay.h>
    
    int main()
    {
    while(1)
    {
        printf("Hello\n");
        delay_1ms(5);  // this function should be in header file ? ? ?
    }
    Cheers

    RocketMan

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,606
    Both. The header might be something like this:
    Code:
    #ifndef DELAY_H_
    #define DELAY_H_
    /* delay.h */
    void delay_1ms(int amount);
    void delay_10ms(int amount);
    void delay_1sec(int amount);
    void delay_1min(int amount);
    #endif
    and the source file might be something like this:
    Code:
    /* delay.c */
    #include "delay.h" /* May not be necessary in this case. */
    void delay_1ms(int amount) {}
    void delay_10ms(int amount) {}
    void delay_1sec(int amount) {}
    void delay_1min(int amount) {}
    Of course, I would rather have one delay function that delays in multiples of a small quantum instead of multiple delay functions that delay in multiples of different quanta.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Ok thanks, I will give it a go.

    Cheers

    RocketMan

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    I only use header files for all the common stuff between the c files, I never put actual code in it. Just prototypes, #defines, and typedefs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. functions in .C file not recognized in .CPP file
    By tooKool4School6 in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2006, 10:30 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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