Lior
Lior

Reputation: 6051

Static functions and variables in C

I know what is the purpose of using static variables in an object oriented language, still, I don't understand what is the meaning of using the "static" keyword in C. Can someone explain it to me?

Upvotes: 0

Views: 303

Answers (2)

Nocturno
Nocturno

Reputation: 10027

The value that a static variable has upon leaving a function is the same value that variable will have the next time the function is called.

A static function can be called only from within the same file that the function appears.

Upvotes: 1

icktoofay
icktoofay

Reputation: 129119

On a function or global variable, static makes the function or global variable local to that file; other files cannot access that function or global variable by that name (but they can access it if you give a pointer to it away).

On a local variable, it makes it act as if it was a global variable, but is only accessible within that function (unless, again, you give a pointer to it away).

Upvotes: 8

Related Questions