Priyank Kumar Dalal
Priyank Kumar Dalal

Reputation: 117

Can we use same function name in 2 different file in C by giving static?

Can we use same function name in 2 different file in C by giving static? Like static myfunc() in file1.c and static myfunc() in file2.c. Will linker understand the scope or it will throw the error?

Upvotes: 8

Views: 2293

Answers (3)

Kerrek SB
Kerrek SB

Reputation: 477640

Global names declared static have internal linkage, which means that such a name is private to the translation unit. More specifically, within one translation unit, all static declarations of a name refer to the same object or function, but in each translation unit, such a declaration refers to a distinct object or function. (By contrast, all names with external linkage refer to the same entity across the entire program.)

Upvotes: 3

aleroot
aleroot

Reputation: 72686

static tells that a function or data element is only known within the scope of the compilation unit, so the answer to your question is Yes you will be able to declare a static function with the same name and even with the same signature in two different compilation unit.

Upvotes: 10

DarkDust
DarkDust

Reputation: 92432

Yes, this is OK and one of the points of the static keyword.

Upvotes: 5

Related Questions