C Tutorial
Normally include directive in c program is used to include the header files to access predefined functions available in c.
include header file example,
#include<stdio.h>
include stdio.h header file in c program to use standard input and output functions like scanf and printf functions.
include directive is also used to include the another file content in current program.
Include directives treats included file as text and replaces the content before c program compilation.
Lets create define.c file with below content,
#include<stdio.h> #define ch char #define cpf printf
Include the define.c file content in new file define1.c with also main function as below, #include "define.c" void main() { ch name[50] = "codingpointer.com"; cpf("welcome to %s",name); }
Output:
$ cc define1.c $ ./a.out welcome to codingpointer.com
Let's see another example,
define reading input part in input.c file as below,
printf("\nEnter number: "); scanf("%d", &number);
Include reading input part code from input.c to current file define.c
#include<stdio.h> void main() { int number; #include "input.c" printf("\nNunber entered is: %d", number); }
Output:
$ cc include.c $ ./a.out Enter number: 4 Nunber entered is: 4
This c program is used to include the function which is defined in another file.
define compute_area function define.c file,
#include<stdio.h> int compute_area(int radius) { return 3.14*radius*radius; }
include define.c file in current program,
#include "define.c" void main() { int area, radius=10; area = compute_area(radius); printf("\nArea of circle: %d", area); }
Output:
$ cc include.c $ ./a.out Area of circle: 314
This current program works as expected to compute area.
This c program is used to inclde macro defined in another file.
define macro AREA in define.c file,
#include<stdio.h> #define AREA compute_area(radius) int compute_area(int radius) { return 3.14*radius*radius; }
include define.c file in current program to use defined macro in another file,
#include "define.c" void main() { int area, radius=10; area = AREA; printf("\nArea of circle: %d", area); }
Output:
$ cc include.c $ ./a.out Area of circle: 314
int area, radius=10;
This c program is used to include variables defined in another file.
define variables in define.c file,
int area, radius=10;
include efine.c file in current program in variable declaration location.
#includeint compute_area(int radius) { return 3.14*radius*radius; } void main() { #include "define.c" area = compute_area(radius); printf("\nArea of circle: %d", area); }
Output:
$ cc include.c $ ./a.out Area of circle: 314
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page