Discussion: Files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in main C program, like for e.g. to include stdio.h in C program to use function printf()   Exercise 2. Creating myhead.h : Write the below code and then save the file as myhead.h or you can give any name but the extension should be .h indicating it’s a header file.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Discussion: Files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in main C program, like for e.g. to include stdio.h in C program to use function printf()

 

Exercise 2. Creating myhead.h : Write the below code and then save the file as myhead.h or you can give any name but the extension should be .h indicating it’s a header file.

 

// It is not recommended to put function definitions 

// in a header file. Ideally there should be only

// function declarations. Purpose of this code is

// to only demonstrate working of header files.

void add(int a, int b)

{

    printf("Added value=%d\n", a + b);

}

void multiply(int a, int b)

{

    printf("Multiplied value=%d\n", a * b);

}

 

Including the .h file in other program : Now as we need to include stdio.h as #include in order to use printf() function. We will also need to include the above header file myhead.h as #include”myhead.h”. The ” ” here are used to instruct the preprocessor to look into the present folder and into the standard folder of all header files if not found in present folder. So, if you wish to use angular brackets instead of ” ” to include your header file you can save it in the standard folder of header files otherwise. If you are using ” ” you need to ensure that the header file you created is saved in the same folder in which you will save the C file using this header file.

 

Using the created header file :

// C program to use the above created header file

#include <stdio.h>

#include "myhead.h"

int main()

{

    add(4, 6);

 

    /*This calls add function written in myhead.h  

      and therefore no compilation error.*/

    multiply(5, 5);

 

    // Same for the multiply function in myhead.h

    printf("BYE!See you Soon");

    return 0;

}

Output:

 

Added value:10

Multiplied value:25

BYE!See you Soon

 

NOTE : The above code compiles successfully and prints the above output only if you have created the header file and saved it in the same folder the above c file is saved.

 

Source: Dimpy Varshni (GeeksforGeeks)

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Parenthesis
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education