Search code examples
cgccgcc-warning

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


I have been getting following warning while compiling the C source code in the gcc 4.6.1.

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

I refered to this link Wunused but could get exactly what is causing this warning.Would anybody tell me in more details what is causing this warning and how can We get rid of it?

[EDIT] I have a following snippet of code. The compile shows the above mentioned warning. Could you please suggest me how can correct it?

   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.
   // } 

Solution

  • You need to include the preprocessor guards around the declaration and initialisation of BoolTest:

    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
    

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