the difference   between .c  and .h file

fhaider used Ask the Experts™
Am just biging to lern C++ , I have java experience .

I want to know what the purpose of the Header files (.h and .hpp) and for what I use them .

I will be appreciate  if there is simple example

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
programmatically they are same. no difference.
its just that h files are header files which has usually function definitions in it, e.g stdio.h header file has definition of printf function,
if you open stdio.h you will see printf defintion there.
C is just C source code file.


thanx  drequinox:

so can I for example make file name newstdio.c and copy the contain of the stdio.h in the top of newstdio.c
(insteading of #include stdio.h) ? dose the compiler will accept such c file ?

Hi fhaider,

When deciding whether an item should go into a .c or .h file, consider this:

--  If the item uses memory (it is a variable or function) it goes in the .c file
--  If it is a declaration (typedef, struct, extern, enum, #define, etc.) it should go in a .h file.

If a program is relatively small an consists of only 1 source (.c) file it is common to place the declarations into the .c file instead of writing a separate .h file.

Good Luck,
Top Expert 2009
In C++ terms its also good to remember to put templates in a .h file since they are technically declarations. Templates are a special case, sort of like an inline function, both declaration and definition combined.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial