Anik Shahriar Anik Shahriar - 21 days ago 5
C Question

Storing a function in a header file

I have this structure as a header file in one of my c projects.

struct info{

char name[50];
char address[150];
char country[20];
char region[20];
char postalcode[10];
char phonenumber[15];
char paymethod[15];

};


But can i store a function in a header file? Cause it'd be easier to maintain the whole program.

Answer

You can write a function and save it in a header file with the extension .h, so that you can use it in times. As example, i will create a function to add two integer values and return the sum whenever the function is being called..

int sumOfTwoInteger(int a ,int b){
    return a+b;
}

Now all i have to do is save the file with a name and an extension .h, e.g 'myheader.h' Now put the header file in the same directory where your actual program is. In the beginning of your actual program add the header file

#include"myheader.h"

Now you can call that function whenever you want...

int sum;
sum = sumOfTwoInteger(10,5);

And the sum will be stored in the variable 'sum'