File Inclusion (creating our own header files)
(i). #include<filename>
(ii). #include”filename”
#include<filename>:
The specified file is searched in the specified list of directories mentioned with the path , if the file is found in the specified list of directories (i.e. path) then it is included in the program and program executes correctly otherwise an error gets generated.
#include “filename”:
The specified file is searched in
(a). The specified list of directories mentioned with the path
(b). In the current directory
If the file is found in either of the above places, it is included in the program and program executes correctly otherwise an error gets generated.
Example:
Write a C program to create your own header files and include them in the program.
File: 1
To include required header files
File: 2
To include macro definitions
File: 3
To include some function required in the program
File: 4
To include some function required in the program
File: 5
C program with main function, in which all the above files are included.
File:1
Filename: head.a (user defined name)
Points to remember:
1. Save the file with the name “head.a” (user defined name)
2. Any number of files can be included
3. Any number of such files can be created
4. Do not compile the program , just save the file.
#include<stdio.h> #include<conio.h> //clrscr() getch() #include<ctype.h> #include<dos.h> #include<stdlib.h> //exit() #include<string.h>
File:2
Filename: macro.b (user defined name)
Points to remember:
1. Save the file with the name “macro.b” (user defined name)
2. Any number of macros can be included
3. Any number of such files can be created
4. Do not compile the program , just save the file.
#define hello void #define ankur main() #define c clrscr(); #define g getch();
File:3
Filename: fun1.cpp (user defined name)
Points to remember:
1. Save the file with the name “fun1.cpp” (user defined name)
2. Any number of functions can be included
3. Any number of such files can be created
4. Do not compile the program , just save the file.
void show_date_time() { struct time t; struct date d1; getdate(&d1); gotoxy(10,3); printf("Todays date is (dd-mm-yy) : %d-%d-%d\n",d1.da_day,d1.da_mon,d1.da_year); gotoxy(10,4); gettime(&t); printf("Todays time is:%2d:%02d:%02d.%02d\n",t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); printf("\n\n\n"); } int square(int n) { return(n*n); } int cube(int n) { return(n*n*n); } void table(int n) { int i,t; for(i=1;i<=10;i++) { t=n*i; printf("%d * %d = %d\n",n,i,t); } }
File:4
Filename: fun2.cpp (user defined name)
Points to remember:
1. Save the file with the name “fun2.cpp” (user defined name)
2. Any number of functions can be included
3. Any number of such files can be created
4. Do not compile the program , just save the file.
void pass() { char n[20],ch; int i=0,j; clrscr(); gotoxy(1,10); printf("Enter the password : "); gotoxy(30,10); j=30; while((ch=getch())!='\r') { gotoxy(j,10); printf("*"); j++; n[i]=ch; i++; } n[i]='\0'; if (strcmp(n,"hello")==0) printf("\n\n\nthe password is correct\n"); else { printf("\n\nInvalid password , try again\n"); getch(); exit(1); //header file : stdlib.h } } int max(int n1,int n2,int n3) { if (n1>n2 && n1>n3) return(n1); else if(n2>n3) return(n2); else return(n3); } int fact(int n) { int i,f=1; for(i=1;i<=n;i++) { f=f*i; } return(f); }
File:5
Filename: main.c (user defined name)
Points to remember:
1. Save the file with the name “main.cpp” (user defined name with extension name as .c or .cpp)
2. Compile the program and execute it.
#include"head.a" #include"macro.b" #include"fun1.cpp" #include"fun2.cpp" hello ankur { int op=0,a1,a2,a3,a4; pass(); while(op!=6) { clrscr(); show_date_time(); printf("1. Square of a number \n"); printf("2. Cube of a number \n"); printf("3. Table of a number \n"); printf("4. max of 3 numbers \n"); printf("5. Fact of a number \n"); printf("6. Exit\n"); printf("Enter your choice "); scanf("%d",&op); switch(op) { case 1: printf("Enter any number "); scanf("%d",&a1); a4=square(a1); printf("Square = %d\n",a4); break; case 2: printf("Enter any number "); scanf("%d",&a1); a4=cube(a1); printf("cube = %d\n",a4); break; case 3: printf("Enter any number "); scanf("%d",&a1); table(a1); break; case 4: printf("Enter 3 numbers "); scanf("%d %d %d",&a1,&a2,&a3); a4=max(a1,a2,a3); printf("max number is %d\n",a4); break; case 5: printf("Enter any number "); scanf("%d",&a1); a4=fact(a1); printf("fact = %d\n",a4); break; case 6: printf("end\n"); break; default: printf("Invalid choice\n"); break; }//switch getch(); } //while } //main