473,761 Members | 9,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2840
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*****@mindsp ring.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_glo bal.net> wrote in message
news:7Y******** *********@newss vr14.news.prodi gy.com...
"pete" <pf*****@mindsp ring.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************ *********@g47g2 000cwa.googlegr oups.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************ *********@g47g2 000cwa.googlegr oups.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
4614
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
6
5834
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
3980
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 bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
15
6592
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
9
6367
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 keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
11
3843
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
9
2179
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 variables?
14
6024
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
2
3776
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 common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
28
8886
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 didn't find any good explanation about it. Could the experts please give me an explanation or point me to some link? Thanks! /Why Tea
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.