Skip to main content

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

Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.

This article will look closer at the header file and main function in C programming language.

This article will look closer at the header file and main function in C programming language.

This is a very beginner level of tutorial for those students who find it difficult to understand what a header file is and how important main function is 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 they contain functions and macros. A header file 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. If we want to use any of these functions and macros, then we have to include a 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 suggests, is the most important function in every C or C++ program. It is an entry point or starting point of program execution. C compilers 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 a 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 programs I can give as example to show you the importance of “main()” in C language.

As you can see in the output window screenshot, that only function testOne(), testTwo() and testFour() executed. We called testOne(), testFour() from “main()”, but why were 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 programs at that time, compiler searches for “main()” function—and if no main function is found, then it throws error.


Sample Program Output

Sample Program Output

This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.



Hardev kumar on April 29, 2015:

Cc programming is very easy.

Naveed Ahmed from Bahrain on January 04, 2015:

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?

Arunpradhap on August 02, 2013:

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

msoprano from UK/LONDON on January 24, 2012:

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.


vijay singh on January 08, 2012:

nice example

Jeet on November 29, 2011:

Thanks for sharing.

LABINO on October 16, 2011:

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

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on October 16, 2011:

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 :)

Karan Singh on October 08, 2011:

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...

pandi.shc on September 27, 2011:

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

shahid on September 11, 2011:

raj bahi these tutorial helping me a lot so thanks

RAMAN on September 04, 2011:

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

Robert MacDonald on May 15, 2011:

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

shekhar on March 28, 2011:

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

RAJKISHOR SAHU (author) from Bangalore, Karnataka, INDIA on March 17, 2011:

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

speedbird from Nairobi, Kenya on March 13, 2011:

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