Why can't i make static function

W

Who_Knows_Geek

I got 1 set of library, say 'mylib.c' and 'mylib.h'. I define 2
functions (pub_f() and priv_f()) in 'mylib.c' and just declare pub_f()
in 'mylib.h' (i assume the priv_f() is private for that library -
internal use).

Then I create an application, called 'diary.c', and include 'mylib.h'.
but, WHY i still can call the priv_f() function from 'diary.c',
although i don't declare it in 'mylib.h'?

Thanx,

Truely geek :)
 
J

Jack Klein

I got 1 set of library, say 'mylib.c' and 'mylib.h'. I define 2
functions (pub_f() and priv_f()) in 'mylib.c' and just declare pub_f()
in 'mylib.h' (i assume the priv_f() is private for that library -
internal use).

Then I create an application, called 'diary.c', and include 'mylib.h'.
but, WHY i still can call the priv_f() function from 'diary.c',
although i don't declare it in 'mylib.h'?

Thanx,

Truely geek :)

If you want to make priv_f() not callable by name from other
translation units (roughly, source files), use the static keyword.
 
B

bd

Who_Knows_Geek said:
I got 1 set of library, say 'mylib.c' and 'mylib.h'. I define 2
functions (pub_f() and priv_f()) in 'mylib.c' and just declare pub_f()
in 'mylib.h' (i assume the priv_f() is private for that library -
internal use).

Then I create an application, called 'diary.c', and include 'mylib.h'.
but, WHY i still can call the priv_f() function from 'diary.c',
although i don't declare it in 'mylib.h'?

Thanx,

Truely geek :)

If you call a function without a prototype, the compiler assumes it returns
int and takes whatever you gave it. If it turns out it doesn't, the results
are undefined. If you want a function to be only accessible from the file
it was defined in, put 'static' in the definition, like so:

static void priv_f() {
printf("Hello, world!\n");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top