Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 363
msc Avatar asked Dec 17 '16 06:12

msc


People also ask

What is the difference between static and void in C?

static − Here, the object is not required to access static members. void − This states that the method doesn't return any value. main − is As stated above, it s the entry point of a C# program i.e. this method is the method that executes first.

What is static void function in C?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

What is the difference between void and function?

A void function doesn't return any value. An int function, returns an integer value unlike void. Also, float ones return float value, char one return char... etc.

What does static void mean?

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. void means that the method has no return value. If the method returned an int you would write int instead of void .


1 Answers

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.

like image 150
artm Avatar answered Oct 13 '22 10:10

artm