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 Reply Children
  • "Being able to declare variables is a feature of C++."

    Errr... I think you missed a bit there?!

    Something like: "Being able to define variables in the middle of a block is a feature of C++."

    Both declaring and defining variables are, of course, entirely possible in 'C'! ;-)

  • 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.

  • Thank you all very much for your know how and
    for Your effort.

    I learned a lot and I hope I will be learning
    a lot more here as well as in other threads.

    Regards
    J.

  • Note that what you have learned here is standard ANSI 'C' - nothing specifcally to do with Keil nor the 8051!