Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress the compiler warning on unused variable in C?

For a peripheral requirement in an embedded system, I have to declare a variable to read a register, but won't use the value later. Hence, I get compiler warning about my unused variable naturally. How can I suppress the warning? I have 2 ways in mind:

  1. using compiler directives, I hesitate because they are compiler-dependent
  2. adding a dummy read from variable, like:

    volatile int var;
    
    var = peripheral_register;
    
    var = var;
    

Do you have a better idea?

like image 873
Reza Amani Avatar asked Nov 02 '16 02:11

Reza Amani


1 Answers

If all you need to do is read the register (to clear some status flag for example), then you do not need a receiving variable at all just:

(void)peripheral_register ;

is sufficient assuming the register is itself is declared volatile so that it must be read.

Otherwise you could make your dummy var global with external linkage - that way the compiler cannot determine that it is not read elsewhere; but that's a far uglier solution.

like image 144
Clifford Avatar answered Sep 29 '22 07:09

Clifford