0

I have a header file that looks like

header.h

int TOS;

This file is being included by just one code file

code.c

#include "header.h"
TOS=0;

When compiling code.c GCC issues a warning

code.c:3:1: warning: data definition has no type or storage class [enabled by default] code.c:3:1: warning: type defaults to ‘int’ in declaration of ‘TOS’ [enabled by default]

I fail to understand the cause of this warning. Isn't it equivalent to declaring and defining TOS in code.c? i.e.

code.c

int TOS;
TOS=0;
Safwan Ahmad
  • 373
  • 1
  • 3
  • 10

3 Answers3

2

It is because you define TOS in the global scope, which need you to define the type of TOS(it is an declaration), if no type was given, by default it is int.

This will cause an conflicting type error,

char x;
x = 0;
lostyzd
  • 4,515
  • 3
  • 19
  • 33
2

The correct way to forward a variable in a header file would be

extern int TOS;

without the extern this could otherwise result that TOS is allocated in several compilation units (.o files).

You'd then give a definition in one .c file as

int TOS;

This would then reserve space for it and since it is a variable in global scope it also would initialize it to 0. If you want to make this initialization explicit or if you want it to be to another value than 0, the correct syntax for initialization (and not assignment) is

int TOS = 54;

Modern C doesn't allow the syntax that you seem to have inherited from somewhere, namely a definition of a global variable with implicit type int.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • While `TOS` would have been allocated in several translation units, in C (but not in C++), the linker should collapse them into one. – ninjalj Oct 09 '11 at 15:20
  • @ninjalj, no I don't think so. This would only be the case when there is no initialization, I think. **If an identifier declared with external linkage is used in an expression .. somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.** – Jens Gustedt Oct 09 '11 at 19:24
  • `int TOS;` from `header.h` is a tentative definition with external linkage. You can include `header.h` from several translation units and the linker will collapse them into one. – ninjalj Oct 10 '11 at 18:07
0

TOS=0 is not an assignment, it's a declaration with an initializer (i.e: a definition). int TOS; is a tentative definition with external linkage. When the linker links several translation units together, it collapses the corresponding object (=memory for a variable). As said elsewhere, the default type of int is a C89 feature absent from later editions of the standard.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • Nope. First there is no such thing like a tentative declaration in the C standard, there are *tentative definitions*. And then tentative definitions require that there is no initializer: **A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition.** – Jens Gustedt Oct 09 '11 at 19:28