0

when i'm reading some tutorial in C programming,all of them said that we need to put only structure type declarations, function prototypes, and global variable extern declarations, in the .h file; and for the function definitions and global variable definitions and initializations we need to put them in the .c file. but when i try to put the content of the function in the header file it works fine. if it works fine why we must not use it ?
in the sum.h :

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include<stdio.h>
#include<stdlib.h>

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

#endif // HEADER_H_INCLUDED

int the main.c:

#include "sum.h"
int main()
{
    printf("%d\n",sum(1,2));
    return 0;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Ryuuk
  • 89
  • 1
  • 3
  • 14

1 Answers1

-1

Broadly, you might what to include your header in multiple source files. If you have actual function definitions in there, you're going to have multiple definitions. This will be fine until the linker tries to assemble your compiled sources into an executable or library, at which point it will fail because it finds several functions with the same name.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15