Warning: variable but not used [-Wunused-but-set-variable]

I get the following warning when compiling C source code in gcc 4.6.1 .

  warning: variable set but not used [-Wunused-but-set-variable] 

I referenced this Wunused link, but could get exactly what causes this warning. Someone will tell me in more detail what causes this warning, and how can we get rid of it?

[EDIT] I have the following code snippet. Compilation shows the above warning. Could you suggest me how to fix this?

  test_function(){ BOOL BoolTest; BoolTest = test_fucntion2(); #ifdef CHECK if (!BoolTest) { misc_StartErrorReport(); misc_ErrorReport("\n test_function2: Input not indexed.\n"); misc_FinishErrorReport(); } #endif // BoolTest is no more used below it. // } 
+6
source share
5 answers

You need to include preprocessor wrappers around the BoolTest declaration and initialization:

 test_function() { #ifdef CHECK BOOL BoolTest = test_function2(); #else test_function2(); #endif #ifdef CHECK if (!BoolTest) { misc_StartErrorReport(); misc_ErrorReport("\n test_function2: Input not indexed.\n"); misc_FinishErrorReport(); } #endif 

(it is assumed that you still want to call test_function2() , even if CHECK not defined, presumably for its side effects - if not, then you do not need the #else section, and you can combine two #ifdef blocks into one).

+5
source

You set a variable, but do not use it. For instance:

 int foo = some_expression; //...code which does not use foo 

You can simply delete it.

+5
source

Setting a variable assigns a value to it (possibly implicitly)

 int main(void) { int local1, local2; local1 = 0; /* local1 set to 0 */ local2 = 0; /* local2 set to 0 */ return 0; } 

In the above program, both variables were set to a value, but they were not used. If you replace the second line with

  int local2 = local1; 

now I used the local1 variable - and the warnings should only be 1.

To get rid of the warning, remove the assignment from your code. This may in turn generate other warnings ... :)

+4
source

This means that you assign a value to a variable, but then you will never read that value later in your code (hence the word "installed but not used"). For instance:

 int useful = 10; int useless = 3; if (useful) { //Do stuff } 

Note that you specify useful and useless , but you only read the value in useful . Usually, when I receive this message, it means that I forgot about the variable or found a way to embed a statement that no longer needs this variable.

+2
source

You have not used BoolTest. You can see that there is no difference between your code and

 test_function(){ #ifdef CHECK if (!test_fucntion2()) { 
0
source

Source: https://habr.com/ru/post/895370/


All Articles