Header File and Main Function in C
By rajkishor09
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>
| No Photo |
The C++ Programming Language by Bjarne Stroustrup (1986, Paperback)
Current Bid: $.99
|
|
|
C Programming Language by Dennis M. Ritchie and Brian W. Kernighan
Current Bid: $14.95
|
|
|
C Programming Language (2nd Edition) by Brian W. Kerni
Current Bid: $27.98
|
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()
{
testTwo();
printf("this is test 4.\n");
}
void main()
{
clrscr();
testOne();
testFour();
getch();
}
Explaination
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.
Output
Header File and Main Function in C
Is this little tutorial helpful to understand main functioon and header file?
See results without votingUseful Links
- A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different..... - Your First Turbo C Program
You might be thinking that you have learnt lot about C language background and now eagerly want to try your first program in C. To do so first of all you need a C compiler, I have been using Turbo C++ compiler for this purpose. Why Turbo C++......... - How to swap two numbers without using third (temp) variable
This article explains how we can swap variable values without using third variable. There are different ways to do so and I tried including all know methods. You can get code in C, Java and C# language. - What is Function in C Language?
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program.... - C Programming - Recursion in C language
We have learnt different types function C language and now I am going to explain recursive function in C. A function is called “recursive” if a statement within body of that function calls the same function for example look at below code: - Array in C programming – Programmer's view
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int, float, and char. So an integer array can..... - How to work with Two Dimensional Arrays in C
We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two dimensional arrays (2D arrays) to store values..... - How to work with Multidimensional Array in C Programming
C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array having two dimensions called 2D array....... - How to work with Structure in C program
We used variable in our C program to store value but one variable can store only single piece information (an integer can hold only one integer value) and to store similar type of values we had to declare many variables. To overcome this problem..... - Arrays of Structures in C Programming
Welcome back my readers; in this tutorial I am going to explain Arrays of Structures. This tutorial is advancement to my previous topic“Structure at Work”; if you missed the basic of structure then you can read it here. - Pointers in C Programming
In this tutorial I am going to discuss what pointer is and how to use them in our C program. Many C programming learner thinks that pointer is one of the difficult topic in C language but it’s not completely true. It is difficult to understand in.... - C Programming - Dynamic Memory Allocation in C
This tutorial is intended to tell beginner the power of pointers in C programming. This tutorial will try to explain how with the help of pointer we can solve the problem of memory management. If you don’t know what pointer is in C language then..... - C Programming – Function Pointer in c language
Like C variables, function too has address and we can use this address to invoke function. So this tutorial is entirely devoted to function-pointer. But before we can call a function using we need to find out its address. So first we will see how.... - Turn Your PC Keyboard to Musical Keyboard using C Programming
To accomplish this task you dont need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you... - File Copy Program in C Language
Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok lets start. The main...
Comments
@speedbird: i write these for beginners only, if it helps others then its my luck. thanks for visiting, take care.
this is not good example to tell about main importance in c & c++
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
i can very easily understand the importance of main() from your example thanks
raj bahi these tutorial helping me alot so thanks
Yea it's really super.Even if it's a basic question i don't know before this now i can easily understood .
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...
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 :)
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
Thanks for sharing.
nice example
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


speedbird 14 months ago
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