Thread: .h file vs .c file

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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