This blog is under construction

Wednesday, 23 May 2012

File inclusion in C language

File inclusion directive is used to include the contents of standard header files or user written program files to another file.  Usually, angle braces are used to include standard library header file and double quotes are used to include user written program files.

     #include <stdio.h> => includes the contents of standard header file "stdio.h"
     #include "add.c"    => includes the contents of user written program file "add.c"

File inclusion C example:

  // add.c - user written program file
  #define SIZE 5

  int add(int a, int b) {
          return (a + b);

  // fileinclusion.c
  #include <stdio.h>  // standard library header file
  #include "add.c"     // user written program file

  int main() {
        int a[SIZE], i, sum =0;
        printf("Enter any 5 nos to perform addition\n");
        for (i = 0; i < SIZE; i++) {
                scanf("%d", &a[i]);
        for (i = 0; i < SIZE; i++) {
          /* calling add() - function in add.c */
                sum = add(sum, a[i]);
        printf("Sum of 5 numbers: %d\n", sum);
        return 0;

  jp@jp-VirtualBox:~/$ ./a.out
  Enter any 5 nos to perform addition
  12   23   34   45   56
  Sum of 5 numbers: 170

No comments:

Post a Comment