This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

declaration after function call

I just started using kile c51 compiler with µVision.
My problem is only a stylistic issue.

While initializing my controler:
<dir>

  unsigned char dummy1 = 0xFF;
  /* call openADC0 */
  openADC0( dummy1 );

  unsigned char dummy2 =  0xFF;
  /* call openADC0 */
  openUART0( dummy2 );
</dir>
Why am I not allowed to daclare another
variable, after I called a function! ;(

Is there any compiler optimization I can use?

Regards
J.

Parents
  • From the snippet given, I don't see any point to having the variable at all, instead of passing the constant 0xFF directly to the function. But perhaps there's more to the actual code.

    ANSI C does permit declaration of local blocks, inside of which you can declare variables. This is an often overlooked feature of C. It's sometimes handy to constrain the scope of temporary variables. The following is legal ANSI C.

         {
         unsigned int data dummy1 = 0xFF;
    
         func1 (dummy1);
         }
    
         {
         unsigned int data dummy2 = 0xFF;
    
         func2 (dummy2);
         }
    
    Note that you can similarly declare variables inside any open brace of a block -- after an if, inside a while loop, and so on.

    Unfortunately, C51 doesn't really take advantage of the information about locality the programmer has provided. For memory allocation purposes, it essentially just promotes the locals, so the above code will take up 4 bytes of "stack" when it really only needs 2.

Reply
  • From the snippet given, I don't see any point to having the variable at all, instead of passing the constant 0xFF directly to the function. But perhaps there's more to the actual code.

    ANSI C does permit declaration of local blocks, inside of which you can declare variables. This is an often overlooked feature of C. It's sometimes handy to constrain the scope of temporary variables. The following is legal ANSI C.

         {
         unsigned int data dummy1 = 0xFF;
    
         func1 (dummy1);
         }
    
         {
         unsigned int data dummy2 = 0xFF;
    
         func2 (dummy2);
         }
    
    Note that you can similarly declare variables inside any open brace of a block -- after an if, inside a while loop, and so on.

    Unfortunately, C51 doesn't really take advantage of the information about locality the programmer has provided. For memory allocation purposes, it essentially just promotes the locals, so the above code will take up 4 bytes of "stack" when it really only needs 2.

Children