• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Static funtions

Hi
  Please explain me abt Static functions in C...
  Static variables which hold the values
with in the program,,But what is use with Static functions..

Is there static functions are available in C.. Ans is Yes then give me syntax and use of that functions with examples...


Thanks in advance
Mahesh
0
MaheshKP
Asked:
MaheshKP
  • 3
  • 2
  • 2
  • +1
1 Solution
 
KangaRooCommented:
static functions can only be used in the .c module in which they are defined, they are not visible in other modules.
0
 
MaheshKPAuthor Commented:
Hi Rao
   What ur telling is Static functions are available in C , Just like static
variables..My understand is correct means give me a ex..
0
 
rbrCommented:
The static modifier is a little bit different between functions and variable and it also depends on the scope.
Example


a.c

static int a=1;

static int func1 (void)
{
    static int b=0;
    b+=a;
    return (b)  
}

func1 only can be called out of the source a.c. If you link another source with it func1 will be invisible from the other source. This is he mean of a static function.

Every time the func1 is called the variable b will be incremented. This is the mean of a  static variable within a function.

a can only be accessed within a.c. In another soure a extern int a will not work (or will use another variable).
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
KangaRooCommented:
Actually, I'm not even sure they are avavilable in ANSI C (I do mostly C++) but  GCC thinks it is.
0
 
Andrei RodionovCommented:
MaheshKP!

In C language static functions are used for code hiding: static functions defined in the module are visible in that module *only*, i.e. linker can't see they though any other "ordinary" function is visible for linker.

So static functions are useful in multimodule projects.

Andrew
0
 
KangaRooCommented:
Example? just at the static keyword:

static void f()
{
   /*Ur code */
}


That's what I said, can only be used from the same module, isn't it?
0
 
Andrei RodionovCommented:
KangaRoo, you are right.

Andrew
0
 
MaheshKPAuthor Commented:
ThanQ for all

Mahesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now