Header Files (.h) and Main Function in C Programming Language
The Header File and Main Function: A Tutorial for Beginners
This is a very beginner-level 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 tasks 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 the 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
The 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
Explanation
So when you run the above program, you will see the below output. I thought this is one of the simplest programs I can give as an example to show you the importance of “main()” in C language.
As you can see in the output window screenshot, only functions testOne(), testTwo() and testFour() are executed. We called testOne(), testFour() from “main()”, but why were we able to see the output of testTwo()? Because we called testTwo() in testFour(), a chain of function calling is created; main() is called testFour() and testFour() is called testTwo(). We cannot see the output of testThree() because neither we call it from main() nor from other functions called by main(), so it is excluded from program execution.
I think this is a very simple and powerful example of the importance of the 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, the compiler searches for “main()” function—and if no main function is found, then it throws an error.
Related Articles
- Multi-Dimensional Arrays (3D Arrays) in C Programming Language
C allows an array of two or more dimensions. More dimensions in an array mean more data can be held.
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.
© 2011 RAJKISHOR SAHU
Recommended
Comments
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.
Soprano
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