473,396 Members | 2,003 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,396 software developers and data experts.

static function?

Hello,

I found some function like

void static foo(...){
....
}

what does the static keyword stand for? what is a static function?

thanx
Nov 14 '05 #1
12 2467
dual0 <du***@bluewin.ch> scribbled the following:
Hello, I found some function like void static foo(...){
...
} what does the static keyword stand for? what is a static function?


Enforces static linkage. This means that this function cannot be called
directly from other translation units, because they won't know its name
(it's not linked against them). You can still pass a function pointer
pointing to it and call through that.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #2
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.

Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?
Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
nethlek
Nov 14 '05 #4
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?
Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
nethlek
Nov 14 '05 #5
In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote:
But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?


I do that as a rule of thumb. It doesn't hurt.

(kinda pseudo-code)

/* main.c */

/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
int main()
{
a();
b();
}

If the program grows, some of the static could become external functions. In
that case, they will loose the 'static', and some prefix will probably added
to their name. The separated compilation process will be implemented
(separated TU, header etc.)

/* main.c */

#include x.h"

/* private functions */
static a()
{
}

/* entry point */
int main()
{
a();
X_b();
}

/* x.h */
X_b();
/* x.c */
/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
X_b()
{
a();
b();
}

Here are the skeletons I use for code layouts :

Interface (.h)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

Implementation (.c)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #6
In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote:
But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?


I do that as a rule of thumb. It doesn't hurt.

(kinda pseudo-code)

/* main.c */

/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
int main()
{
a();
b();
}

If the program grows, some of the static could become external functions. In
that case, they will loose the 'static', and some prefix will probably added
to their name. The separated compilation process will be implemented
(separated TU, header etc.)

/* main.c */

#include x.h"

/* private functions */
static a()
{
}

/* entry point */
int main()
{
a();
X_b();
}

/* x.h */
X_b();
/* x.c */
/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
X_b()
{
a();
b();
}

Here are the skeletons I use for code layouts :

Interface (.h)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

Implementation (.c)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
ne*****@tokyo.com (Mantorok Redgormor) writes:
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
>Hello,
>
>I found some function like
>
>void static foo(...){
>...
>}
>
>what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon?


I don't think so.
Do other people here(comp.lang.c) do this?


Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #8
ne*****@tokyo.com (Mantorok Redgormor) writes:
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
>Hello,
>
>I found some function like
>
>void static foo(...){
>...
>}
>
>what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon?


I don't think so.
Do other people here(comp.lang.c) do this?


Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #9
Mantorok Redgormor wrote:
.... snip ...
Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something which
is frowned upon? Do other people here(comp.lang.c) do this?


I tend to mark all functions as static whenever possible, i.e.
whenever I do not explicitly want access to them from other
modules. The advantage is that I can immediately see that such
functions are not referenced externally, no matter how the overall
system is modified, and thus that I can limit usage checks to the
single module during maintenance.

This also implies that such static functions _do not_ appear as
prototypes in any header file. I can think of no reason the word
static should ever appear in a header.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
Nov 14 '05 #10
Mantorok Redgormor wrote:
.... snip ...
Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something which
is frowned upon? Do other people here(comp.lang.c) do this?


I tend to mark all functions as static whenever possible, i.e.
whenever I do not explicitly want access to them from other
modules. The advantage is that I can immediately see that such
functions are not referenced externally, no matter how the overall
system is modified, and thus that I can limit usage checks to the
single module during maintenance.

This also implies that such static functions _do not_ appear as
prototypes in any header file. I can think of no reason the word
static should ever appear in a header.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
Nov 14 '05 #11
In <cu*************@zero-based.org> Martin Dickopp <ex****************@zero-based.org> writes:
Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.


What's appropriate for a monolithic program is not necessarily still
appropriate when the program is split into multiple source code files.
To minimise the pain, just don't use static in the monolithic program.
Only *after* splitting you can decide what should be declared as static.

The right thing is to try to anticipate whether the program will remain
a monolithic program for the rest of its life or whether it should be
designed as a multiple source code file program from the very beginning
(even if its initial version doesn't really justify a multi source file
approach). My crystal ball is excellent for this kind of guessing ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <cu*************@zero-based.org> Martin Dickopp <ex****************@zero-based.org> writes:
Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.


What's appropriate for a monolithic program is not necessarily still
appropriate when the program is split into multiple source code files.
To minimise the pain, just don't use static in the monolithic program.
Only *after* splitting you can decide what should be declared as static.

The right thing is to try to anticipate whether the program will remain
a monolithic program for the rest of its life or whether it should be
designed as a multiple source code file program from the very beginning
(even if its initial version doesn't really justify a multi source file
approach). My crystal ball is excellent for this kind of guessing ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean-Francois Brault | last post by:
I wrote a crappy class for radian angle management. The class consists of an array of radian values. I put all these things in a class in which all methods are static, so I can access it anywhere...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
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...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
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...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
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...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.