[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
modem » Borland » CppVclCodeSnippets » JTAG » RSS » CFscanf » open source » CppHelloWorld » IBM » CFprintf » CHeaderFile

(C) Header file

A file called in your program, using the #include statement.

Header files often consist of functions or  ?struct  ?declarations and or definitons. They have the .h extension.

Sometimes a header (.h) file also has an implementation (.c) file with the same name. When this is the case, the header file contains declarations and the implementation file the definitions. A header file and its implementation file are sometimes called a  ?Unit.

  1. include <stdio.h> /* Header in standard folder */
  2. include "myOwnCoolHeaderFile.h" /* Local header */


Note that the second #include first checks if the header file is in the same folder as the program. If it is not present, it will check the standard header folder for it.

Always use #include guards [1].

Use of header and implementation files

There are three ways to put your code in header (.h) and implementation (.c) files:
  • 1) Use only .h files
  • 2) Add .cpp files to project
  • 3) #include all .cpp files from the file that contains main()

1) Use only .h files

This option is used by many libraries (most notably the C++ STL) put all definitions in the .h files. This has the advantage that they are easy to call: you only need to #include the necessary .h files.

//Example.h
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
  1. include <stdio.h>
void sayHello() { printf("Hello"); /* Definition in .h file */ }
  1. endif


//Main.c
  1. include "Example.h"
int main() { sayHello(); return 0; }


2) Add .cpp files to project

This option is used in larger non-library code. The header files contain the declarations, the implementation files the definitions. The .cpp files must be 'Added to Project'. How this is done is dependent on the programming environment. The advantage is that compiling is quickest.

//Example.h
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
void sayHello();
  1. endif


//Example.c
  1. include "Example.h"
  2. include <stdio.h>
void sayHello() const { printf("Hello"); }


//Main.c
  1. include "Example.h"
int main() { sayHello(); return 0; }


3) #include all .cpp files from the file that contains main()

This option can be used in example source code. It enables newbies to compile-and-run.

//Example.h
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
void sayHello();
  1. endif


//Example.cpp
  1. include "Example.h"
  2. include <stdio.h>
void sayHello() { printf("Hello"); }


//Main.c
#include "Example.cpp" //Calling .cpp file
int main()
{
  sayHello();
  return 0;
}


'Header' links



last edited (November 27, 2006) by bilderbikkel, Number of views: 2391, Current Rev: 1

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.