ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on July 7, 2018
rajkishor09 profile image

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.

what is header file and main function in C language
what is header file and main function in 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>

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

Header File and Main Function in C

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

See results

© 2011 RAJKISHOR SAHU

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Hardev kumar 

      3 years ago

      Cc programming is very easy.

    • Naveed Ahmed 624 profile image

      Naveed Ahmed 

      3 years ago from Bahrain

      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?

    • profile image

      Arunpradhap 

      5 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

    • msoprano profile image

      msoprano 

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

      Soprano

    • profile image

      vijay singh 

      6 years ago

      nice example

    • profile image

      Jeet 

      6 years ago

      Thanks for sharing.

    • profile image

      LABINO 

      7 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

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      7 years ago from Bangalore, Karnataka, INDIA

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

    • profile image

      Karan Singh 

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

    • profile image

      pandi.shc 

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

    • profile image

      shahid 

      7 years ago

      raj bahi these tutorial helping me a lot so thanks

    • profile image

      RAMAN 

      7 years ago

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

    • profile image

      Robert MacDonald 

      7 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

    • profile image

      shekhar 

      7 years ago

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

    • rajkishor09 profile imageAUTHOR

      RAJKISHOR SAHU 

      7 years ago from Bangalore, Karnataka, INDIA

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

    • speedbird profile image

      speedbird 

      7 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

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)