13

see in one project source code i have seen belows declaration

static int *foo();

so it declare foo as static function returning pointer to int. So here i wana ask you whats the purpose of declaring function as static ?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

5 Answers5

16

The function's name isn't visible outside the translation unit (source file) in which it's declared, and won't conflict with another function foo in another source file.

In general, functions should probably be declared static unless you have a specific need to call it from another source file.

(Note that it's only the name that's not visible. It can still be called from anywhere in the program via a pointer.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    What is the difference between marking a function as static and simply not including it in the header? Won't omitting it from the header have the same effect? Or, is there a reason to not put something in the header and also not mark it as static? – Alex Oct 18 '11 at 21:40
  • 3
    @Alex: Including its declaration in the header makes it visible to the compiler, and thereby to other source files that include that header. Making it non-`static` makes its name visible to the linker; other source files can access it by declaring it themselves. As a matter of style, either (a) declare it static, or (b) declare it non-static *and* include its declaration in the corresponding .h file. (If C had a full-fledged module system, guidelines like this wouldn't be necessary.) – Keith Thompson Oct 18 '11 at 22:28
6

It prevents other translation units (.c files) from seeing the function. Keeps things clean and tidy. A function without static is extern by default (is visible to other modules).

cnicutar
  • 178,505
  • 25
  • 365
  • 392
4

From the C99 standard:

6.2.2 Linkages of identifiers

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.

and

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

Community
  • 1
  • 1
znkr
  • 1,746
  • 11
  • 14
3

Declaring a function as static prevents other files from accessing it. In other words, it is only visible to the file it was declared in; a "local" function.

You could also relate static (function declaration keyword, not variable) in C as private in object-oriented languages.

See here for an example.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
3

Marking a function or a global variable as static makes it invisible to the linker once the current translation unit is compiled into an object file.

In other words, it only has internal linkage within the current translation unit. When not using static or explicitly using the extern storage class specifier, the symbol has external linkage.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130