Operating System - HP-UX
1828360 Members
3299 Online
109976 Solutions
New Discussion

static in front of function declared locally

 
Max_4
Frequent Advisor

static in front of function declared locally

Hello everybody,

To express my question, I will help myself
with the following simple snippet
(all functions residing in the same file):


/** stat_fn.c **/

#include

void foo1(void)
{
void foo2(void);

printf("foo1\n");
foo2();
}

static void foo2(void)
{
printf("foo2\n");
}

int main(void)
{
foo1();

return 0;
}

When I compile it using gcc I get:

$ gcc -g -Wall -ansi -pedantic -o stat_fn stat_fn.c
stat_fn.c:12: warning: `foo2' was declared `extern' and later `static'

And then I thought, maybe changing the declaration for foo2()
withing foo1() to 'static void foo2(void);' would silence the
warning. Then the new file would look like this:

/** stat_fn1.c **/

#include

void foo1(void)
{
static void foo2(void);

printf("foo1\n");
foo2();
}

static void foo2(void)
{
printf("foo2\n");
}

int main(void)
{
foo1();

return 0;
}

But to my surprise now I got the following:

$ gcc -g -Wall -ansi -pedantic -o stat_fn1 stat_fn1.c
stat_fn1.c: In function `foo1':
stat_fn1.c:5: warning: invalid storage class for function `foo2'

Why is 'static' an invalid storage class for function `foo2' in
the declaration for foo2() embedded in foo1()?

After that I found out, that one way to silence all warnings was to
put the declaration for foo2() at file scope, like this:


/** stat_fn2.c **/

#include

static void foo2(void);

void foo1(void)
{
printf("foo1\n");
foo2();
}

static void foo2(void)
{
printf("foo2\n");
}

int main(void)
{
foo1();

return 0;
}


And now zero warnings appeared...

Does this mean that I cannot declare a function with the storage
class 'static' if this declaration is local to a function?
What does the standard say about this?

Any light on this matter will be highly appreciated...

Thanks a lot in advance...

Max
3 REPLIES 3
Renda Skandier
Frequent Advisor

Re: static in front of function declared locally

hi Max,
It's mostly because you are using the ansi switch. "True ansi" requires you to declare all functions extern or static but will default to extern, assuming there might be an include file declaring the function. I believe if you ran your program thru "lint" , you would be flooded with warnings about all functions not declared at the top of your program.
Paddy_1
Valued Contributor

Re: static in front of function declared locally

I think its okay to declare a function static in a local context.

The problem is that you need to remove the forward reference too foo2 in foo1 and everything should run okay.

The sufficiency of my merit is to know that my merit is NOT sufficient
Max_4
Frequent Advisor

Re: static in front of function declared locally

After doing some reading I found that ISO C99
standard makes a pretty clear point of it:


6.7.1 Storage-class specifiers
...
5 The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern.
...

Thanks anyway...