declare static function? 
Author Message
 declare static function?

Hi every one,
     I am reading the Steven's APUE book.
In page 179, It declared function as static like this

static void f1(int i, int j, int k);

I wonder why we need the static here.
What I learned from school is every function in C is a static function.
or
Does the declaration mean we put a function as global? just like
what we do on variables?
static int k;

Would you please tell me the semantics of that declaration?
Thank you guys
Dean



Wed, 07 Sep 2005 02:00:33 GMT  
 declare static function?

Quote:
> Hi every one,
>      I am reading the Steven's APUE book.
> In page 179, It declared function as static like this
> static void f1(int i, int j, int k);
> I wonder why we need the static here.
> What I learned from school is every function in C is a static function.
> or
> Does the declaration mean we put a function as global? just like
> what we do on variables?
> static int k;
> Would you please tell me the semantics of that declaration?
> Thank you guys
> Dean

In C "static" means different things than what it means for example in
Java. In Java "static" means that the method is outside any instance
and "directly" in the class.
C has none of that stuff. In C "static" means that the function is, by
name, only visible in the same translation unit. If another translation
unit has a static function with the same name, those are different
functions. Although static functions can be referred to across
translation units by pointers. This also goes for static variables.
C has an additional rule about this. INSIDE a function, a static
variable has a different meaning. It's a variable that, once set to a
value, retains that value between invocations, unless it is changed
again.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
   - Ernst Jan Plugge



Wed, 07 Sep 2005 02:09:47 GMT  
 declare static function?

Quote:

> Hi every one,
>      I am reading the Steven's APUE book.
> In page 179, It declared function as static like this

> static void f1(int i, int j, int k);

> I wonder why we need the static here.
> What I learned from school is every function in C is a static function.

I can't comment on what you learned in school, but in the context of C,
that particular use of 'static' is a 'linkage' specification.  The
'static' keyword causes the function 'f' to have 'internal linkage'.
The net effect of 'internal linkage' is that the function is not visible
outside of the translation unit in which it is defined. Translation unit
essentially means that source file, after includes have been imported.  

Quote:
> or
> Does the declaration mean we put a function as global? just like
> what we do on variables?
> static int k;

Static means a lot of different things in C.  You can have static local
variables too, and that means something different from static file-scope
variables, so I'm not sure what you mean there.

Quote:

> Would you please tell me the semantics of that declaration?

It means the opposite of 'extern'.  The function is only visible and
useable[*] in the file in which it is defined.  

[*] you can involve some trickery to use the function in another
translation unit, if you really want to.  It involves returning a
pointer to the static function to a external translation unit via a
helper function with external linkage.

HTH,
Ryan.



Wed, 07 Sep 2005 02:02:08 GMT  
 declare static function?


Quote:

> >      I am reading the Steven's APUE book.
> > In page 179, It declared function as static like this

> > static void f1(int i, int j, int k);

> > I wonder why we need the static here.
> > What I learned from school is every function in C is a static function.

(snip of meaning of static in this case)

Quote:

> Static means a lot of different things in C.  You can have static local
> variables too, and that means something different from static file-scope
> variables, so I'm not sure what you mean there.

The word static is supposed to mean non-moving.  Why C gave it this meaning
in this case, I have no idea.

Note that PL/I has the STATIC/AUTOMATIC/CONTROLLED/BASED storage allocation
attributes, as well as INTERNAL/EXTERNAL storage scope attributes.
Different names for different attributes.

There are many features of C that may, or may not, have been borrowed from
PL/I, one of the more popular block structured languages at the time C and
its ancestors were being created.

(Comments welcome from those who know the history better.)

-- glen



Wed, 07 Sep 2005 04:52:58 GMT  
 declare static function?

Quote:



[snip]

Quote:
> The word static is supposed to mean non-moving.  
> Why C gave it this meaning
> in this case, I have no idea.

> Note that PL/I has the STATIC/AUTOMATIC/CONTROLLED/BASED
> storage allocation attributes, as well as
> INTERNAL/EXTERNAL storage scope attributes.
> Different names for different attributes.

Sounds hopelessly arcane.  Um.. moreso than C, I mean ;)

Quote:
> There are many features of C that may, or may not,
> have been borrowed from PL/I, one of the more popular
> block structured languages at the time C and its
> ancestors were being created.

> (Comments welcome from those who know the
> history better.)

I would also be very interested to hear more about the languages that
inspired C.  Having not been alive in the 60s (or 70s for that matter),
I'm not quite sure what "PL/1" even stands for.  Procedural Langugage
maybe?

Thanks,
Ryan.



Wed, 07 Sep 2005 05:29:26 GMT  
 declare static function?

Quote:

> Hi every one,
>      I am reading the Steven's APUE book.
> In page 179, It declared function as static like this

> static void f1(int i, int j, int k);

> I wonder why we need the static here.
> What I learned from school is every function in C is a static function.

Wrong.

Quote:
> or
> Does the declaration mean we put a function as global? just like
> what we do on variables?
> static int k;

k can only be accessed in the same file it was defined in.

Quote:
> Would you please tell me the semantics of that declaration?

I think you're reversed the definition of 'static'. Static, at file scope,
specifies that the function or veriable defined cannot be accessed outside
of that translation unit, aka file.

--
Freenet distribution (temporary): http://24.25.175.161:8891/BVQQm34LfFc/
A boss with no humor is like a job that's no fun.



Wed, 07 Sep 2005 06:56:47 GMT  
 declare static function?

Quote:
> In C "static" means different things than what it means for example in
> Java. In Java "static" means that the method is outside any instance
> and "directly" in the class.
> C has none of that stuff. In C "static" means that the function is, by
> name, only visible in the same translation unit. If another translation
> unit has a static function with the same name, those are different
> functions. Although static functions can be referred to across
> translation units by pointers. This also goes for static variables.
> C has an additional rule about this. INSIDE a function, a static
> variable has a different meaning. It's a variable that, once set to a
> value, retains that value between invocations, unless it is changed
> again.
> --

> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
> \----------------------------------------- Finland rules! ------------/
> "The day Microsoft makes something that doesn't suck is probably the day they
> start making vacuum cleaners."
>    - Ernst Jan Plugge

I want to thank you all.
Java is the language I work with, and
I thought the Java "static" is the same as C "static
That's why I got comfused.
Big Mistake.... :-p


Wed, 07 Sep 2005 07:00:34 GMT  
 declare static function?

Quote:


>>The word static is supposed to mean non-moving.  
>>Why C gave it this meaning
>>in this case, I have no idea.

>>Note that PL/I has the STATIC/AUTOMATIC/CONTROLLED/BASED
>>storage allocation attributes, as well as
>>INTERNAL/EXTERNAL storage scope attributes.
>>Different names for different attributes.

> Sounds hopelessly arcane.  Um.. moreso than C, I mean ;)

>>There are many features of C that may, or may not,
>>have been borrowed from PL/I, one of the more popular
>>block structured languages at the time C and its
>>ancestors were being created.

>>(Comments welcome from those who know the
>>history better.)

> I would also be very interested to hear more about the languages that
> inspired C.  Having not been alive in the 60s (or 70s for that matter),
> I'm not quite sure what "PL/1" even stands for.  Procedural Langugage
> maybe?

Ryan...

The name of the language is written "PL/I" (with the 'I'
presumably being the roman numeral 'one'. I recall the primary
roots for PL/I being fortran and Algol (but that influence also
came from a number of other languages and variants of FORTRAN).

More or less concurrent with the development of PL/I was the
development of PL/S (Programming Language - Systems) initially
intended for internal IBM use only; but provided to a limited
number of IBM customers. PL/S was (more or less) PL/I with the
ability to use register names as variables and to incorporate
assembly code as part of its natural syntax.

If you're interested in this flavor or archaeology, you might try
to track down a copy of Jean E. Sammet's "Programming Languages".
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c



Wed, 07 Sep 2005 08:50:44 GMT  
 declare static function?
I did some experiments on "static" storage class specifer.
I just want to verify two things
1) if static is used in function definition, it means that
   the function name is not to be exported to the linker.
   is it correct?
2) the term "static extent" is only applicable to variable
   declaration, not to function declaration right?
Thank you
Dean


Wed, 07 Sep 2005 09:32:06 GMT  
 declare static function?

Quote:


> I did some experiments on "static" storage class specifer.
> I just want to verify two things
> 1) if static is used in function definition, it means that
>   the function name is not to be exported to the linker.
>   is it correct?

If static is used at file scope (whether on a function declaration or a
variable declaration), it limits the scope of that identifier to the
current file.

Quote:
> 2) the term "static extent" is only applicable to variable
>   declaration, not to function declaration right?

If by "static extent", you mean "static storage duration", meaning that
the lifetime of the object is the lifetime of the entire program, then
yes, it doesn't apply to functions because in C, functions aren't
objects.  Their lifetime is always that of the entire program.

When static is used in block scope, it causes the object designated by
that definition to have static storage duration.  You can't use it with
a block-scope function declaration.

        - Kevin.



Wed, 07 Sep 2005 09:52:38 GMT  
 declare static function?

Quote:

> I did some experiments on "static" storage class specifer.
> I just want to verify two things
> 1) if static is used in function definition, it means that
>    the function name is not to be exported to the linker.
>    is it correct?

Yes, essentially. It may also trigger some other optimisations, such as
inlining. But note that you can still call a static function between
translation units, so long as you have a pointer to the function.

a.c:
#include <stdio.h>
static void foo(void) {printf("Foo!\n");}
void (*bar(void))(void) {return foo;}

b.c:
void (*bar(void))(void);
int main(void) {bar()(); return 0;}

Quote:
> 2) the term "static extent" is only applicable to variable
>    declaration, not to function declaration right?

A variable declared inside a function is a local variable. Local variables
always have no linkage. By default, a local variable has "automatic storage
duration". Adding the static keyword to the definition of a local variable
makes it have "static storage duration".

A variable declared outside a function is a file-scope variable. File-scope
variables always have static storage duration. By default, a file-scope
variable has "external linkage". Adding the static keyword to the definition
of a file-scope variable makes it have "internal linkage".

A function definition has file scope. Functions have no defined storage
duration, but are essentially static. By default, a function has external
linkage. Adding the static keyword to the defintion of a function makes it
have internal linkage.

--
Simon.



Wed, 07 Sep 2005 12:07:46 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Declaring static function returning pointer to extern function

2. The Implication of Declaring a Function as Static

3. declaring functions as static in Turbo C?

4. One Problem in Calling Static function from Non static function

5. Accessing non-static functions from a static function

6. declaring functions inside other functions

7. Why functions declared within functions?

8. declared static but never defined, Huh ?

9. Static declared as parameter.

10. declaring static arrays

11. Why declare returned pointers static?

12. Declaring Static Members of a class

 

 
Powered by phpBB® Forum Software