By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

static functions

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

Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
prasi wrote:

hi, all
I wanted to know the significance of static functions.
can anybody explain me.


They have internal linkage.

--
pete
Nov 15 '05 #2

P: n/a
"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
prasi wrote:

hi, all
I wanted to know the significance of static functions.
can anybody explain me.


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,

--
Mabden
Nov 15 '05 #3

P: n/a
Mabden wrote:
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

Nov 15 '05 #4

P: n/a
"Ulrich Eckhardt" <do******@knuut.de> wrote in message
news:3q************@uni-berlin.de...
Mabden wrote:
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.


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...

--
Mabden
Nov 15 '05 #5

P: n/a

"Mabden" <mabden@sbc_global.net> wrote in message
news:7Y*****************@newssvr14.news.prodigy.co m...
"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
prasi wrote:
>
> hi, all
> I wanted to know the significance of static functions.
> can anybody explain me.
They have internal linkage.


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.

<snip irrelevant ramblings> HTH,


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

Nov 15 '05 #6

P: n/a

prasi wrote:
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.

Nov 15 '05 #7

P: n/a

In article <11*********************@g47g2000cwa.googlegroups. com>, "John Bode" <jo*******@my-deja.com> writes:
prasi wrote:
I wanted to know the significance of static functions.


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.

--
Michael Wojcik mi************@microfocus.com

Even 300 years later, you should plan it in detail, when it comes to your
summer vacation. -- Pizzicato Five
Nov 15 '05 #8

P: n/a

Michael Wojcik wrote:
In article <11*********************@g47g2000cwa.googlegroups. com>, "John Bode" <jo*******@my-deja.com> writes:
prasi wrote:
I wanted to know the significance of static functions.


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.


Good catch. Thanks.

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.