Header Files (.h) and Main Function in C Programming Language

Beginner to expert skill development.
Beginner to expert skill development. | Source

This is a very beginner level of tutorial for those students who find it difficult to understand about what header file is and how important is main function in in any c or C++ program. I hope this will help them a lot.

Header Files

Every C compiler provides a library of around 200 or more predefined functions and macros which we can use in our C program. These library functions or inbuilt functions help programmers to perform common programming task rapidly and efficiently. These functions include input output operation, storage allocation, file handling, string manipulation etc. To use those functions we need to include some files in our program. These files are known as Header Files and it contains functions and macros. A Header Files usually has an extension of .h, like stdio.h, conio.h etc.

In short, a header file, in C or C++, is a collection of functions and macros and if we want to use any of these functions macros then we have to include Header File containing function definition. For example, if we want to use “printf()” function then we have to include “stdio.h” Header File.

Syntax: #include <header file name>

Example : #include <stdio.h>

Main Function

Main function as the name suggest is most important function in every C or C++ program. It is an entry point or starting point of program execution. C compiler only recognize “main()” function for execution nothing else. If your user defined function call is not included in “main()” function then it will never be executed during program execution. Here is simple program to show what I just said :

Simple main() example

#include <stdio.h>
#include <conio.h>

void testOne()
	printf("this is test 1.\n");
void testTwo()
	printf("this is test 2.\n");
void testThree()
	printf("this is test 3.\n");
void testFour()
        printf("this is test 4.\n");	

void main()



So when you run the above program then you will see below output. I thought this is one of the simplest program I can give as example to show you the importance of “main()” in C language. As you can see in output window screenshot that only function testOne(), testTwo() and testFour() executed. We called testOne(), testFour() from “main()” but why we are able to see the output of testTwo(). Because we called testTwo() in testFour() so a chain of function calling is created; main() called testFour() and testFour() called testTwo(). We cannot see the output of testThree() because neither we called it from main() nor from other function called by main(), so it is excluded from program execution. I think this is a very simple and powerful example of importance of main function in any C or C++ program.

In simple words, programs of C and C++ must have main function because program execution starts from “main()” function. When we compile program at that time compiler searches for “main()” function and if no main function is found then it throws error.



Header File and Main Function in C

Is this little tutorial helpful to understand main functioon and header file?

See results without voting

More by this Author

Comments 16 comments

speedbird profile image

speedbird 5 years ago from Nairobi, Kenya

I did C and C++ some time back but with the advent of Visual Basic I almost forgot about C and C++. Your hub really took me down the memory lane of this two programming language. Thanks for sharing your knowledge. voted UP and rated USEFUL

rajkishor09 profile image

rajkishor09 5 years ago from Bangalore, Karnataka, INDIA Author

@speedbird: i write these for beginners only, if it helps others then its my luck. thanks for visiting, take care.

shekhar 5 years ago

this is not good example to tell about main importance in c & c++

Robert MacDonald 5 years ago

I looked at your discussions re What is header file and main function in c and c++ program. Correct syntax for declaring main is my issue at moment. Quick question: Would you be agreeable to let me send you a tiny c file I need a "small amount of help" with? I can offer $15/hr. (work done from home at your convenience). Paym. by PayPal

RAMAN 5 years ago

i can very easily understand the importance of main() from your example thanks

shahid 5 years ago

raj bahi these tutorial helping me a lot so thanks

pandi.shc 5 years ago

Yea it's really super.Even if it's a basic question i don't know before this now i can easily understood .

Karan Singh 5 years ago

Hi rajkishor, this is a best example to understand importance of main() function...i am come out from lots of problems from when i started to read your examples... i will recommended to all my friend....good job.. keep it up to share your knowledge.. thx a lot...

rajkishor09 profile image

rajkishor09 5 years ago from Bangalore, Karnataka, INDIA Author

Thank you guys for your valuable comments. I am happy that my articles are helping you in your study. Keep visiting and please try to spread this to your friends. Take care, happy learning :)

LABINO 5 years ago

Your tutorial is very clear to me because i had been taught this very C program while i was in school though its a bonus course for me then. you try...........God bless you

Jeet 5 years ago

Thanks for sharing.

vijay singh 5 years ago

nice example

msoprano profile image

msoprano 5 years ago from UK/LONDON

Hi rajkishor, you are a star and smart. thank you very much for your time and passion of inspiring others. i liked your tutorials. i read books but here i get explanation more than in my books. i have some questions that i might ask later to get clarification.

One more time thanks.


Arunpradhap 3 years ago

Dear rajkishor ,

is it possible the C file having main ( ) function to be the header file of another C file having main ( ) function.could you explain

thanks in advance

Naveed Ahmed 624 profile image

Naveed Ahmed 624 2 years ago from Pakistan

I am confused as I am getting errors trying to write my first C program in Dev C++ compiler / editor. The editors reports an error about using the header file iostream.h, while I was taught by my tutor (via video lectures) to include this header file for writing some initial programs to do some analyzing / calculations etc. As per the tutors advise, I am using Dev C++ compiler but it is returning errors.

How do I come about this situation and execute / run my programs without errors?

Hardev kumar 22 months ago

Cc programming is very easy.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

    Click to Rate This Article