Do filenames matter?

.c or .cpp, .h or .hpp?

Just a short diversion to look at filename conventions.

.c
C code - like the Hello World program. .c files should not normally contain features of C++ like object oriented code, class inheritance etc. Definitions of functions should only be put in .c files if there is no chance that you'll want to use that function in any other program. .c files can be compiled in C and C++ projects but if a .c file contains C++ code, it could cause compiler errors.

.h
Definitions of C functions to be reused in a modular way by other programs. Header files are distributed with the compiler and with libraries and other programs. Standard header files can be included into your C files using < > notation, just like the Hello World example program:
#include <stdio.h>
To include your own files (which are not located in standard or privileged folders) use " " notation:
#include "../include/myheader.h"
Note that local headers like this need to have the path to the file specified in full.

.cpp
C++ code. .cpp files can contain a mixture of C and C++ code and include code to deal with objects, classes, inheritance and are common in GUI programming to allow the programmer to make best use of the available GUI interface.

.hpp
Very uncommon - there is little C++ that needs to be in a special header and the use of two separate header filename types will only cause confusion and errors. All C++ programs will need to include some standard C header files, so it is safer to use .h files for all headers, whether C or C++.

other formats
C/C++ projects will contain a mixture of other file formats, project files, Makefiles, documentation, configuration scripts, etc. Many of these will be created by your compiler or development program. Others will be required to package your program for distribution.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.