How to solve "Variable declared but not used" warning in C and CPP

In your C and CPP code, when you compile the code you may get some warnings like "Variable declared but not used".

What it is??

It is a warning shown by gcc compiler, which says a variable declared in the code but not used inside it. Consider the following program.

extern int a; //will show warning "Variable a declared but not used"
void main() {
    int x = 5; //will show warning "Variable x declared but not used"
    ........

How to solve??

You can avoid this warning from your compiler by,

If the variable inside a function

You can use the variable inside a function with if statements. So that you can use the variable meanwhile compiler will not show the warning.

int x = 5;
if(x != 5) {
//Log error
}  

If it is a global function

You can say "this variable is not used now but some other file may use this and ignore this warning" to compiler by using __attribute__((unused)) in the declaration.

extern int a __attribute__((unused));

This method will work on keil and some major working environment.


Cheers!!!!
-> Let Us Embed <-