473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

static functions

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
8 2802
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
"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
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
"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

"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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.