+ 1

How do i create my own header file in c ?

Plse plse explain with example

22nd Jan 2019, 3:12 PM
Programmer Raja
Programmer Raja - avatar
7 Answers
+ 2
1 create a c file without main funtion 2 save the file with .h extendion I am creating a sample headerfile sample.h ======= int sum(int a, int b) { return a+b; } int sub(int a,int b) { return a-b; } int mul(int a,int b) { return a*b; } int div(int a,int b) { if(b==0) return 0; else return a/b; } int mod(int a,int b) { if(b==0) return 0; else return a%b; } ================ using headerfile ============ #include “sample.h” void main() { int a,b; clrscr(); printf(“Enter two values :”); scanf(“%d%d”,&a,&b); printf(“The sum of %d and %d is %d\n”,a,b,sum(a,b)); printf(“The sub of %d and %d is %d\n,a,b,sub(a,b)); printf(“The mul of %d and %d is %d\n”,a,b,mul(a,b)); printf(“The div of %d and %d is %d\n”,div(a,b)); printf(“The mod of %d and %d is %d\n”,a,b,mod(a,b)); getch(); }
23rd Jan 2019, 8:34 AM
sree harsha
sree harsha - avatar
+ 2
Refer to the following: filename.h: #define FILENAME_H // code filename.cpp: #include "filename.h" // code
22nd Jan 2019, 7:17 PM
Dread
Dread - avatar
+ 1
Expansion on example defining FILENAME_H: The practical use for defining the filename is to prevent multiple inclusions of the same header file into one piece of code. Sometimes multiple headers use a library requiring a specific header file. The filename in all caps is defined inside an if statement. // myfile.h #ifndef MYFILE_H #define MYFILE_H #define sqrx(x) ((x)*(x)) #endif The ifdef prevents the same header from being loaded multiple times. You may use any name for your header but it must end in .h or .hpp (optional extension for C++) Your defined name should be the file name replacing invalid characters with an underscore. This will help to prevent unintentionally skipping a necessary include. For example if every header defined HEADER_H and every header tested for it, then only one header file would ever be included. The same directives can be used in C and C++. This makes it unnecessary to remember multiple ways to accomplish the same goal.
1st Feb 2019, 1:12 AM
Roger Greenlaw
+ 1
sree harsha why we need to create a header file without main can you explain plse
1st Feb 2019, 1:31 PM
Programmer Raja
Programmer Raja - avatar
+ 1
The header file provides definitions for macros, prototypes for functions in a library, constant definitions and functions that you may use. Typically library function prototypes are provided by the library creator and given the same name as the library with the extension as .h or .hpp. If you create a function or macro that you want to use in several different programs you should consider creating a specific header for those functions and definitions. When you have done this the code is available for you to use in any program or module you create. Editing your personal header files is a good first step toward creating reusable code. This will save you time in future projects if you take the time to clearly document what your header provides. If youfind yourself creating a large number of functions in one header file consider grouping them by type into multiple header files to keep the sizes manageable. Don't forget to include those additional header files when you go back and modify existing sources.
1st Feb 2019, 10:35 PM
Roger Greenlaw
+ 1
Also, naming a header with an .hpp extension is only necessary if the header contains C++ code.
1st Feb 2019, 10:37 PM
Roger Greenlaw
0
Thank you so much to all 👍👍
23rd Jan 2019, 2:17 PM
Programmer Raja
Programmer Raja - avatar