Q:

C program to create your own header file/ Create your your own header file in C

0

C program to create your own header file/ Create your your own header file in C

In this program, we will create our own source (.c) and header file (.h) files and access their function. We will declare associate functions in header file and write code (definition) in source files.

Create your own Header and Source File Example in C

There will be three files

  • myfun.c - Source file that will contain function definitions.
  • myfun.h - Header file that will contain function declaration which have definition in myfun.c source file.
  • main.c – Main Source file that will contain complete code and access the functions which are declared in myfun.h header file.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

myfun.h - Header file

/*
define macro MY_FUN, this will reduce multiple calling
of the header file
*/

#ifndef MY_FUN
#define MY_FUN

/*function declarations*/
int sumOfNumbers(int x, int y);
float avgOfNumbers(int x, int y);

#endif

myfun.c - Source file

/* 
function name: sumOfNumbers()
this function will return sum of two numbers
*/

int sumOfNumbers(int x, int y)
{
    int sum;
    sum = x + y;
    return sum;
}

/*
function name: sumOfNumbers()
this function will return average of two numbers
*/

float avgOfNumbers(int x, int y)
{
    int sum;
    float avg;

    sum = x + y;
    avg = sum / 2;
    return avg;
}

main.c - Main Source file

#include <stdio.h>
#include "myfun.h"

int main()
{
    int a, b;
    int sum;
    float avg;

    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);

    /*calling sumOfNumbers()*/
    sum = sumOfNumbers(a, b);

    /*calling avgOfNumbers()*/
    avg = avgOfNumbers(a, b);

    printf("Sum: %d \nAverage: %f\n", sum, avg);

    return 0;
}

Compiling the program

In turboc Compiler: - Just like normal program compilation, press ALT+F9 to compile and CTRL+F9 to run the program.
In gcc/g++ in Linux Compiler: To compile we can write command gcc main.c myfun.c -o maingcc *.c -o main or gcc -o main *.c .
To Run: write command ./main

Output:

    Enter first number: 10
    Enter second number: 12
    Sum: 22
    Average: 11.000000

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C language important programs ( Advance Programs )

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to remove consecutive repeated character... >>
<< EMI Calculator (C program to calculate EMI)...