warning in extern declaration

The extern declaration in your header file lets modules other than the one in which the variable is defined use it. If it's supposed to be defined as int stack_counter = 0 and live in stack.c, define it like that and put an extern stack_counter in the header.

On line 6 of stack.c, you didn't define a storage class for sroot. Since it's externed in the header, I assume you meant to type snode sroot=NULL.

Fix those, then implement stackpush (make sure it doesn't return void) and deal with the rest of your warnings in order. Note that in C, you have to either use forward declarations of functions (with prototypes) or define your functions before they're used. The cstack function should probably be the last function in the file.


While your code contains a number of rather serious and obvious errors (already covered in other answers), the warning you put into the title of your question is a completely superfluous meaningless warning. GCC compiler is notorious for issuing useless warnings. Many of those warnings seem to be derived from someone's incompetent and completely unsubstantiated belief that doing something is somehow "wrong", while in reality there's nothing wrong with it.

In your case the warning is triggered by

extern int stack_counter = 0;

declaration. Apparently, the "author" of the warning believed that extern specifier should be reserved for non-defining declarations. In this case the presence of initializer = 0 turns the declaration into a definition (and thus formally makes that extern optional). Nevertheless, there's no error in it and, in fact, extern might be quite welcome here to emphasize the fact that stack_counter is intended to be a global variable.

Again, whether you need a global variable here or not is a different question and, again, your code contains a massive number of other errors. But the warning you seem to focus your attention on is not really worth it. Just disable this warning in compiler settings (and, please, write a rude letter about it to GCC team).

Tags:

C