Thread: How to make a .h file?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    How to make a .h file?

    I have some code I'd like to use by having an include statement using Borland C++. It is just C code not C++.

    How do I do this?
    Can you just copy the .c file to a .h extension?
    Are there any Environment Variables I need to set to find the .h file or it there a specific directory it should be in to work?

    Thanks in advance for any help!!!!!

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    all you have to do is put your code into a file that ends in .h . It is generally the best practice to always keep your header file in the same folder as your source file. You dont need to do anything else.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Usually you only keep #define and other preprocessive derectives in a .h file. It is also good to do typedef's and extern. But have c code in there is bad practice if you another c file the use modular programming and include that part in the other program.
    Code:
    #include <stdio.h>
    #include "mycfile.c"

  4. #4
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124
    myheader.h file
    Code:
    #ifndef MYHEADER_H
    #define MYHEADER_H
    
    int x;
    float y;
    enum qt {a, b, c} q;
    //class, structs, function prototypes
    
    #endif
    myheader.c/cpp file
    Code:
    #include "myheader.h"
    
    define functions, methods.
    prog1.c/cpp
    Code:
    #include "myheader.h"
    #include <iostream>
    
    int main(void)
    {
       // call data types and functions from
       // myheader.h and myheader.cpp
      // as need be!
      return(0);
    }
    Also note: DevC++ think projects and M$ Visual Studio think solution!
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Thanks!!!

    Thanks for all the replys.

    I will be trying out your suggestions.

    Thanks again!!!

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: How to make a .h file?

    Originally posted by TR15220
    I have some code I'd like to use by having an include statement using Borland C++. It is just C code not C++.

    How do I do this?
    Can you just copy the .c file to a .h extension?
    Do NOT copy the .c file to a .h!!! A header file should have only declarations in it. It should not have code nor variables defined

    Are there any Environment Variables I need to set to find the .h file or it there a specific directory it should be in to work?

    Thanks in advance for any help!!!!!
    For any subroutines defined that need to be available in other modules add the prototypes to the header file (remove them from the source file). Any global variables that are defined in the source that will be needed elsewhere, add externs to the header for those variables.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM