What is the use of static functions in C lang?

S

sabarish

Hi friend,

what is the use of static functions in c lang? How it can useful to our
programs?
plz help me.
 
M

Mike Wahler

sabarish said:
Hi friend,

what is the use of static functions in c lang? How it can useful to our
programs?
plz help me.

See the answers you already got.

-Mike
 
M

Mark McIntyre

Hi friend,

what is the use of static functions in c lang? How it can useful to our
programs?

"static" restricts the scope of the function to the current file. In
other words, you can't see it from another part of your code.

This is useful for protecting operations which should be private.
 
H

hzmonte

It is recommended that all private functions be declared as static.
With 'static', others can tell at first sight that the function is a
private function - used only by other functions in the same file, but
not by functions in other files. Considering the .c file as an
"implementation" and the corresponding .h file as an "interface", the
programmer prototypes the private functions in the .c and the public
functions in the .h. Of course the definitions of both types of
functions are in the .c file.
 
C

Charles Richmond

Mike said:
See the answers you already got.

This is the second poster asking the same question. They must have the
same professor and the same homework to do...
 
K

Kevin Handy

Charles said:
Mike Wahler wrote:




This is the second poster asking the same question. They must have the
same professor and the same homework to do...
More likely just an automated troller.

If you look you'll see the same questions asked over and over
under numerous names.

Probably by someone who scans these newsgroups to collect
names to be added to the list that they sell to bulk-emailers.
 
J

Joe Estock

Mark said:
"static" restricts the scope of the function to the current file. In
other words, you can't see it from another part of your code.

This is useful for protecting operations which should be private.

Am I the only one who thinks that static should have been local? To me,
the word local makes more sense. Just a thought.

Joe
 
B

Ben Pfaff

Joe Estock said:
Am I the only one who thinks that static should have been local? To me,
the word local makes more sense. Just a thought.

The word "static" has so many meanings in C that no one word is
going to be a meaningful substitute in every situation.
 
J

Jordan Abel

The word "static" has so many meanings in C that no one word is
going to be a meaningful substitute in every situation.

Maybe having a single word with all those meanings was a mistake.
 
J

Joe Wright

Ben said:
The word "static" has so many meanings in C that no one word is
going to be a meaningful substitute in every situation.

So many? I think "static" qualifier has precisely two meanings in C. One
as a storage class and one to suppress external linkage. What are some
of the others?
 
K

Keith Thompson

Joe Wright said:
So many? I think "static" qualifier has precisely two meanings in
C. One as a storage class and one to suppress external linkage. What
are some of the others?

C99 adds a new use of "static" in array parameter declarations.
See C99 6.7.5.3p7.

An example given in the standard is

void f(double a[restrict static 3][5]);

which "specifies that the argument corresponding to a in any call to f
must be a non-null pointer to the first of at least three arrays of 5
doubles".
 
S

Skarmander

Joe said:
So many? I think "static" qualifier has precisely two meanings in C. One
as a storage class and one to suppress external linkage. What are some
of the others?

The problem is that these meanings (three, actually, if you count the
new feature in C99) are only this simple if you think in C, instead of
in terms of the problems you're trying to solve. "Storage class"?
"External linkage"? What planet are these C programmers from, anyway?

static int a;
int b;

void f(int c[static 10]) {
static int d;
int e;
...
}

"Now, you see, the first 'static' means that 'a' has internal linkage,
that is, you can't reference it from other units, unlike 'b'. Of course
'b' has static storage duration, just like 'a', so they're both
initialized to 0.

'c' is a static array argument, in this case, a static array with at
least 10 elements. You don't need to use this, but your program may get
faster if you do. If you use this you must always pass an array with at
least that many elements. 'c' does not have static storage duration, of
course.

'd' is local to 'f', just like 'e' (and 'c', of course), but unlike 'e',
the value of 'd' will be remembered across function calls. 'd' has
static storage duration, like 'b', so it will be automatically
initalized, but 'e' will not.

Got that? Good. And now, C++."

S.
 

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
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top