Search code examples
cstatic-functions

why this error has come ? "static declaration of function follows non-static declaration"?


I am learning about static function and as per rule if i declare function as a static function then i can't access this function into other c file and if i try to access then there should be an error "undefined reference to `fun" so i declare and define static function into add.c and add.h file and call that function into main.c file but i am getting different error i.e "static declaration of 'fun' follows non-static declaration"strong text so my question is that why this error has come ???? please forgive me for my poor English !!!!

/************** main.c****************/
    #include <stdio.h>
    #include <stdlib.h>
    #include "add.h"

    int main(void)
    {
      printf("%d ", fun());
      printf("%d ", fun());
      return 0;
    }

/***************add.c*************/

    #include <stdio.h>
    #include "add.h"

    static int fun(void)
    {
      int a=5,b=4;
      return a+b;
    }

/*********************add.h*************/

    #ifndef ADD_H_
    #define ADD_H_

    static int fun(void);

    #endif /* ADD_H_ */

Solution

  • A static variable or a function is seen only in the file where it is declared.

    C11 standard

    6.2.2.3

    If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.