0

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_ */
  • 3
    Since static functions are file-local, it makes no sense at all to put static function declarations in header files. In this case, you can't make `fun` static in `add.c` if you want to call it from `main.c`. Different files, right? So decide: either (a) static, one file only, not in header file, or (b) non-static, in header file, callable from any file. Choose. – Tom Karzes Dec 01 '17 at 09:37
  • Hey tom thanks for understanding this !!!! – user374112 Dec 01 '17 at 09:43
  • I am tempted to reopen this question because it is not a duplicate of the purported original. This question asks why OP gets an error message stating “static declaration of function follows non-static declaration”, and that is not explained in the original. However, it does not seem the code in the question should generate that message, so there is probably a mismatch between the code and the question. The question should have been clarified or closed with a different reason, such as no longer reproducible or unclear what you are asking. – Eric Postpischil Dec 01 '17 at 13:06

1 Answers1

0

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.

msc
  • 33,420
  • 29
  • 119
  • 214