warning: data definition has no type or storage class

16,826

Solution 1

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.

Solution 2

Declare the variable debug_level as external if it is already declared some where else. Then the compiler will look for the declaration on other places also.

#ifdef DEBUG            
external int debug_level = 6;
#endif

Solution 3

You can't just set the variable in global scope, you actually have make a definition that matches the declaration in the header file:

#ifdef DEBUG            
int debug_level = 6;
#endif
Share:
16,826
Aman Deep Gautam
Author by

Aman Deep Gautam

Software developer at Amazon India. Graduated form IIT Hyderabad in 2013 with honors in Computer Science.

Updated on June 04, 2022

Comments

  • Aman Deep Gautam
    Aman Deep Gautam almost 2 years

    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).