Search code examples
cgccwarningscompiler-warningsgcc-warning

warning: data definition has no type or storage class


I have a file global.h which is included across many files in the project and contain general headers. The relevant contents of file is given below:

#define DEBUG
#ifdef DEBUG
extern int debug_level;
#endif

It has been included in main.c and there is a warning corresponding to the following line in main.c

#ifdef DEBUG            
debug_level = 6;   //compiler generates warning corresponding to this line.
#endif

The warning message issued by compiler is:

src/main.c:14:1: warning: data definition has no type or storage class [enabled by default]
src/main.c:14:1: warning: type defaults to ‘int’ in declaration of ‘debug_level’ [enabled by default]

I do not understand what is that I am doing wrong. Surprisingly the program works fine because I think that compiler assumes that the number is an int(by default).


Solution

  • You should define as int as

    #ifdef DEBUG            
    int debug_level = 6;   //define as int
    #endif
    

    With your code, its implicitly defined as int, hence the warning.

    And extern int debug_level; is not definition but a declaration.