Flag This Hub

Header File and Main Function in C

By


Beginner to expert skill development.
Beginner to expert skill development.
Source: c language

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>

C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
C (Vintage)
Amazon Price: $1.03
List Price: $15.00
Programming in C (3rd Edition)
Amazon Price: $24.00
List Price: $49.99

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

Source: Sample program output

Header File and Main Function in C

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

  • Yes
  • No
  • This tutorial is very concise.
See results without voting

Comments

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

rajkishor09 14 months ago

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

shekhar 14 months ago

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

Robert MacDonald 12 months 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 8 months ago

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

shahid 8 months ago

raj bahi these tutorial helping me alot so thanks

pandi.shc 8 months 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 7 months 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 7 months ago

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

Thanks for sharing.

vijay singh 4 months ago

nice example

msoprano 4 months ago

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

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working