29

I have a two C files.

file1.c

int main()
{
  func(); 
  return 0;  
}

file2.c

static void func(void)
{
  puts("func called");
}

But, if I compile the above code with command cc file2.c file1.c, I got the below,

undefined reference to `func'
collect2: error: ld returned 1 exit status

But, If I remove static keyword inside file2.c and compile the above code with command cc file2.c file1.c, It's succesfully run.

So, I have a question, What is the difference between void and static void function in C?

msc
  • 33,420
  • 29
  • 119
  • 214
  • 11
    A `static` function is not callable from any compilation unit other than the one it is in. Remove the `static` keyword, and the function will be callable from any compilation unit, so your program will link. – Peter Dec 17 '16 at 06:38
  • 3
    Peter's basically right, but there's an advanced scenario where a static function can be called by code in a different source file if a pointer to the function is made available to the code in the other file. You probably don't need to worry about this yet; you probably still have to learn about pointers to functions. The strict version is "a `static` function cannot be called _by name_ from code outside its own translation unit". – Jonathan Leffler Dec 17 '16 at 06:57
  • @Jonathan Leffler: Wow. I was going to answer such Peter, but yes you are right. oh I know how to use a pointer to function, but just, we do not keep in mind all the hacks for particular scenario. Also because, if a function must be called outside, also with a pointer, i am not going to declare it static. Ok the hack, but keep in mind it is an hack and not "you have to learn how to use pointers to function" – jurhas Dec 17 '16 at 10:22
  • @Jonathan Leffler - what you say is correct. The OP gave a simple example that didn't involve making any pointer to the function available, so I didn't bother to describe such a case in my comment. – Peter Dec 17 '16 at 11:15

1 Answers1

57

What is the difference between void and static void function in C?

The real question should be what is the difference between static and non-static function? (the return type void is irrelevant, it can be int or anything else).

The static keyword is somewhat over used. When it applies to function, it means that the function has internal linkage, ie its scope is limited to within a translation unit (simply as a source file).

By default, function is non-static and has external linkage. The function can be used by a different source file.

In your case, the error manifests itself because static func cannot be used in other source file.


When should static functions be used?

static functions are normally used to avoid name conflicts in bigger project. If you inspect Linux kernel source, example in drivers/net you would see many static void functions in there. Drivers are developed by different vendors and the usage of static functions ensure that they can name the functions the way they want without worrying of name conflicts with other non-related driver developers.

Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54
  • The `static` keyword was probably reused in unrelated contexts since introducing new keywords would break existing code using those as names. – Roger Dahl Oct 23 '21 at 01:09