static functions

P

prasi

hi, all
I wanted to know the significance of static functions.
can anybody explain me.
thanks in advance
Prasanna K P
 
M

Mabden

pete said:
They have internal linkage.

They don't move...

I guess I have been using "the new stuff" too much, but can you even
have static ftns in C? Isn't that a C++ / C# thing?

A function with a static variable means that the variable retains the
value it had the last time you called it. So I can initialize a static
variable with 0 in code, and change its value within the function - when
you call it, and the next time you call it, it has the new value, not
zero. If it were not a static variable, it would be zero every time.

ex:
x = add_one();

int add_one(void)
{
static int x=0;
return ++x;
}

X should increment every time you call the function, unless I did
something wrong (which I will be raked over the coals for by random
people with the initials ..)

HTH,
 
U

Ulrich Eckhardt

Mabden said:
I guess I have been using "the new stuff" too much, but can you even
have static ftns in C? Isn't that a C++ / C# thing?

/me believes you're indeed confusing a few things. 'static' has two
meanings in C, one being
A function with a static variable means that the variable retains the
value it had the last time you called it. So I can initialize a static
variable with 0 in code, and change its value within the function - when
you call it, and the next time you call it, it has the new value, not
zero. If it were not a static variable, it would be zero every time.

....the other meaning being when applied to otherwise global variables and
functions, which then get internal linkage, i.e. are only visible by that
very translation unit and don't conflict with equally named variables and
functions in other translation units.

Just since you mentioned other C derived languages, I know that in C++ it
got a third meaning when used on a memberfunction or variable of a class
or struct - in that case it means that the class rather than each instance
of the class class has such an object, so it creates a kind of global
object/function but with a more local scope and a few other small
features.
I also believe that Java uses static to create plain functions because
otherwise it only has memberfunctions, but I'm not sure about that and
it's not really the place to debate that...
Other than that, using static to achieve internal linkage is deprecated
there (should be replaced with the use of anonymous namespaces), in C it
is still accepted and well-understood practice, so you indeed switched a
few things around. ;)

cheers

Uli
 
M

Mabden

Ulrich Eckhardt said:
/me believes you're indeed confusing a few things. 'static' has two
meanings in C, one being


...the other meaning being when applied to otherwise global variables and
functions, which then get internal linkage, i.e. are only visible by that
very translation unit and don't conflict with equally named variables and
functions in other translation units.

That was it. I didn't want to mislead the OP but you're right a static
function just limits it's scope to the file you are in. It is in. I case
there were other .c files that had the same function that did something
different. Not really something I ever saw in 20+ years of programming
in C.

In C# (ON topic - since mentioned in context of the OP's questions and
other followups [btw, I love having to justify what I say to avoid
critique - NOT!])
.... Ahem, in C# static becomes a feature like public, and protected.
Perhaps I should say attribute? I don't know the correct verbiage. It
determines how an object may be used. I think it is useful for
properties (a new thing for C, and c++ users; comes from VB) but I may
be using the wrong terms here...
 
M

Mark B

Mabden said:
They don't move... What?

I guess I have been using "the new stuff" too much,
You're definately smoking too much of 'something'.
but can you even have static ftns in C? Yes.

Isn't that a C++ / C# thing?
No.


it didn't... although, pete's response: 'They have internal linkage'
should have helped ;)
 
J

John Bode

prasi said:
hi, all
I wanted to know the significance of static functions.
can anybody explain me.
thanks in advance
Prasanna K P

In C, a static function cannot be referenced outside of its own
translation unit; the function name is not exported to the linker.

For example, if I have three files, foo.c, bar.c, and main.c, and I
have a static function foo_helper() defined in foo.c, I cannot call
foo_helper() from any of the functions defined in bar.c or main.c.

In a typical module, you'll have a number of support functions that
aren't meant to be called directly from other modules, so you use the
static keyword to make them "private" to the translation unit.
 
M

Michael Wojcik

In C, a static function cannot be referenced outside of its own
translation unit; the function name is not exported to the linker.

Cannot be referenced *by name*. "static" applies to the name of the
function; it doesn't do anything to the function itself. That should
be obvious to any experienced C practitioner, but might not be to
beginners.

So, for example, a static function in one translation unit can be
called from another translation unit through a function pointer,
which might have been returned by a (non-static) function in the same
TU as the static function, or might even be a global variable in
the first TU:

--- inc.c ---
static int inc(int x) { return x+1; }
int (*incfunc)(int) = inc;
---

--- main.c ---
#include <stdio.h>
extern int (*incfunc)(int);
int main(void)
{
printf("%d\n", incfunc(1));
return 0;
}
---

This is actually useful for some abstraction purposes. For example,
in a real inc.c, there might be multiple implementations of the "inc"
function, all of which are hidden (via static) from other TUs. At
program startup, incfunc(x) invokes the default implementation, but a
(non-static) function in inc.c might be available to select a
different one for subsequent calls. Other TUs don't need to know
anything about this mechanism, only the API for using it.
 
J

John Bode

Michael said:
Cannot be referenced *by name*. "static" applies to the name of the
function; it doesn't do anything to the function itself. That should
be obvious to any experienced C practitioner, but might not be to
beginners.

Good catch. Thanks.
 

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,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top