Hi ,

I would thank you for replying for my thread,

I would like to know about static functions C language,what is the use of static functions.

Narendhar Gomathirajan

A static function is a function with the static qualifier applied:

static void foo ( void )
{
  /* Blah blah */
}

What it does is restrict visibility of the function to the translation unit in which it's declared. Functions are implicitly declared as extern by default, which means they're visible across translation units. You can compile the following, but it won't link:

/* file1.c */
void foo ( void )
{
}

extern void bar ( void )
{
}

static void baz ( void )
{
}
/* file2.c */
void foo ( void );
void bar ( void );
void baz ( void );

int main ( void )
{
  foo(); /* OK: foo is extern by default */
  bar(); /* OK: bar is explicitly extern */
  baz(); /* Wrong: baz isn't visible in this translation unit */
}

Hey i am telling you the basic idea about the static functions.
Static functions are those functions which do not have the access to "this" pointer of the class and it can not be declared as virtual.
Main use of the static function is it can be called without crating object of the class.

>Static functions are those functions which do not have the access to
>"this" pointer of the class and it can not be declared as virtual.

You need to learn the difference between C and C++ before resurrecting a three year old thread with completely incorrect and unhelpful information.

Static function are used to avoid the access of the function from the other module.

By default normal functions can access by any module from the file; to avoid we can use static key word to the function.

Also if you have more than one; same functions name across file and if they were in static we will not get any errors.

-Thanks,
Girish.L.C

For the static functions, contrary to what is written in the above posts, for me these are working like a normal functions ( or global functions).
Following code is getting compiled and executed successfully. Could you please advise if I am doing some thing wrong here?

/* file1.c */
void foo ( void )
{
}
extern void bar ( void )
{
}
static void baz ( void )
{
}



/* file2.c */
#include "file1.c"
void foo ( void );
void bar ( void );
void baz ( void );
int main ( void )
{
  foo(); /* OK: foo is extern by default */
  bar(); /* OK: bar is explicitly extern */
  baz(); /* Wrong: baz isn't visible in this translation unit */
}

Thanks in advance,
Tinku

Edited 4 Years Ago by tinku981: Grammar corrections

Could you please advise if I am doing some thing wrong here?

You're doing something wrong by including file1.c in file2.c. Note that Narue was talking about visibility in the translation unit, not source files. A translation unit is the resulting intermediate file after the preprocessor has run, which means that by including file1.c, file1.c and file2.c combined are a single translation unit. Thus all functions are properly defined regardless of their visibility.

i want to ask one thing here that if there is any class and it's one member is static, for example static int a; if then, i included this file in other file twice, then will it give error(redeclaration or something like that) ?

for example static int a; if then, i included this file in other file twice, then will it give error(redeclaration or something like that) ?

The long answer is maybe, but in terms of best practice I'd recommend assuming that the answer is yes. C supports a concept called tentative definitions. Basically as long as there's not an initializer for the object, you can have as many tentative definitions as you want and in the end only one of them will be used:

static int a;

/* All of these are fine and legal */
static int a;
static int a;
static int a;
static int a;

int main(void)
{
    return 0;
}

However, tentative definitions aren't a feature that one should generally take advantage of, which is why I think that one should be aware of them, but otherwise pretend they don't exist.

This article has been dead for over six months. Start a new discussion instead.