Learn C: What are Header Files and How to use with Examples and Interview Questions

 Header Files in C Programming

What are Header Files in C:
  • Header Files contains function declaration and macro definition of C built-in library functions.

  • Header Files can be declared by using .h  extension and within <> angular brackets.

  • Header Files can be included in our C program with the help of preprocessor  directive # to make use of the functions which are declared inside the header files.

  • The header files help in saving time and complexity in finding a specific file. 

 Syntax : #include<filename.h>


There are two types of header files :

  1. System in built header files 
  2. User defined header files


System In Built Header Files: 

It is included in C program within < > brackets.

    Example:  #include<stdio.h> : Header File for standard input and output.

    Let us take an example of printing "Hello World" using two header files. One is "stdio.h" which is used for printing "Hello World" and the another is "conio.h" which is used for holding the console screen.



    Source Code :


    source code in C


        Output :

    source code in C output screen



    User defined Header Files :

    User defined header files are defined within " " double quotes and are defined by the user.

    User defined header files are the combination of many .c extension source files and all these. C extension source files contains any programs which are compiled and linked together to form a single .exe extension file such that all the files which are included uses the header file.

    Example : #include"result.h"

    Let us take an example of  printing the sum of two numbers with the use of User defined header file.

    In this we use two header file one is "printhw.h" which is used to find the sum of two numbers and the other is system defined header file "stdio.h" which is ysed for printing the sum of the two numbers. In this program we don't need to pass the type of the variables as it is previously defined in the function included in the header file "printhw.h".


    Source Code:

    source code for header files

    source code for header files





      Output:

    source code for header files output screen



    Some of the important header files are:

    Name Description
    stdio.h Input/Output Functions
    conio.h Console Input/Output Functions
    stdlib.h General utility functions
    string.h String Functions
    math.h Mathematics Functions
    ctype.h Character handling functions
    time.h Date and Time Functions
    locale.h   Localization Functions
    stdarg.h Variable argument list Functions
    stdbool.h and stddef.h Boolean data type functions, useful types and macros definitions


    Interview Questions on Header Files :

    Q1.  What are header files?

    ans.  Header Files contains function declaration and macro definition for C built-in library functions.
    Header Files can be declared by using .h  extension and within <> angular  brackets.  Header Files can be included in our C program with the help of preprocessor directive # to make use of the functions which are declared inside the header files.


     
    The header files help in saving time and complexity in finding a specific file.                


    Q2.   What is the difference between library functions and user defined functions?

    ans.  Library Functions:
    The functions, which are predefined in any header file, we just include header file and call the functions.
    For example, printf is a library function because it's declaration and definition are exists in the stdio.h header file.
     
    User Define Functions (UDF):
    The functions, which are defined by the programmer. Function’s declarations, callings and definitions are defined by the programmer, any functions which are created by the programmer is known as User Define Functions (UDF's).


    Previous                                                                             Next



    For any query related to the content mail to devkumar082001@gmail.com

    No comments:

    Powered by Blogger.