Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GCC saying a variable is unused when it is not?

Tags:

c

gcc

GCC is saying snr_db is unused when I set -Wall flag, but this variable is used. When i initialize it outside the scope of the for, gcc stops warning me. Does anyone know why is that?

double snr_db;
double snr_db_step = #WHATHEVER

for (int interval = 1, snr_db = 20.0; interval <= intervals; interval++, snr_db -= snr_db_step) {
    snr_lin = pow(10.0, snr_db / 10.0);
    bit_err_rate = ber_bpsk(snr_lin);
    //CODE CONTINUES
}
like image 331
Jefferson Avatar asked Nov 30 '22 08:11

Jefferson


1 Answers

Actually it is not used. You declared it double -

double snr_db; // this is unused 

But in the for loop-

for (int interval = 1, snr_db = 20.0; ..)

In this snr_db is int here (this is same case as int x,y;, both are int as being part of declaration) and over shadows the one declared above this loop body.

Therefore, double snr_db; remains unused.

You could do this-

double snr_db;
int interval; 

for(interval = 1, snr_db =20.0;..){....}  // only intialization
 /* ^^^^^^^^^^^^^^^^^^^^^^^^ this would be a different case from above as here
',' does work as comma operator therefore, evaluating both the expressions. */
like image 64
ameyCU Avatar answered Dec 04 '22 12:12

ameyCU