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

How to use extern sbit declare?

I declare a variable unsigned char bdata Kde in a.c.

[in file a.c]

unsigned char bdata Kde;

Than I want to use the variable inb.c.
[in file b.c]
#include <stdio.h>
.....
extern unsigned char bdata Kde;
sbit testbit=Kde^1;
void main(void)
{......}

:error C141:.......a.c:syntax error near 'sbit'

why?

Parents
  • Mark,

    I tried compiling

     0x04; 

    with the Keil compiler and it is not ignored, it gives a warning:
    ..\MAIN.C(492): warning C275: expression with possibly no effect 
    So that's not something that's removed by optimization at level 0, it's a different topic.

    As I was saying, if I had been able to turn off the optimizer, I would have been able to see whatever it was that the compiler "helpfully" discarded.

    How hard would it be to allow the compiler to finish the job of comparing two constant expressions?

            MOV      A,    #0x04
            XRL      A,    #1
            JNZ      ?C0004
            INC      ERROR_COUNT
    ?0004:
    

    There, we've just compared 0x04 with 1, and if they are equal, we increment ERROR_COUNT.

    I would be surprised if the Keil folks couldn't easily restore this feature. To be sure, I've got other things I'd rather see given priority.

    Robert

Reply
  • Mark,

    I tried compiling

     0x04; 

    with the Keil compiler and it is not ignored, it gives a warning:
    ..\MAIN.C(492): warning C275: expression with possibly no effect 
    So that's not something that's removed by optimization at level 0, it's a different topic.

    As I was saying, if I had been able to turn off the optimizer, I would have been able to see whatever it was that the compiler "helpfully" discarded.

    How hard would it be to allow the compiler to finish the job of comparing two constant expressions?

            MOV      A,    #0x04
            XRL      A,    #1
            JNZ      ?C0004
            INC      ERROR_COUNT
    ?0004:
    

    There, we've just compared 0x04 with 1, and if they are equal, we increment ERROR_COUNT.

    I would be surprised if the Keil folks couldn't easily restore this feature. To be sure, I've got other things I'd rather see given priority.

    Robert

Children
  • How hard would it be to allow the compiler to finish the job of comparing two constant expressions?

    That is not a simple question. You see, the ANSI standard from Section 6.6 states the following:

    "A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be."

    So, a C Compiler may evaluate constant expressions at compile-time rather than at run-time. This is the choice of the compiler developer. It is not an optimization. Evaluating constant expressions at compile-time generates tighter code.

    There is no difference at compile-time or at run-time between (int) 1+2 and (int) 3. Both have the same value.

    The Keil compiler DOES evaluate constant expressions at compile-time. I do not know of a good quality compiler that doesn't.

    The Keil compiler DOES NOT generate code with constants evaluated explicitly at run-time. As stated previously, this is not an optimization. It is an allowed and oft-used part of the ANSI specification.

    I'm certain there are plenty of compilers that generate explicit code for each operation (constant or not, unused or not). But, I would not use them for my applications where speed and code size were important.

    Using the assembler listing generated by the compiler is a great way to help you learn the assembly language or even to help you help the compiler generate better code. But, it is not necessarily the best way to help you validate the meaning of your code. That is best done by a utility like PC-LINT (see http://www.keil.com/pclint/ ) which checks the syntax AND SYMANTECS of the program.

    Jon

  • Jon,

    That is very interesting, but I think you are a little bit in denial about what level 0 is doing. It's discarding big chunks of code if it chooses to do so. I don't see that that can be considered anything but optimization.

    Robert

  •    if (count = 5)
       {      
          count = 1;   
       }
       if (1 == 1)
       {      
          count = 2;   
       }
    
    The first "if" statement generates a warning:
    *** WARNING C276 IN LINE 5 OF MAIN.C: constant in condition expression

    The second "if" statement generates no warning.

  • if (count = 5)
    
    is a constant expression as the condition. You are setting count to the value of 5, not comparing it with 5 (= instead of ==)

    The second expression
    if (1 == 1)
    
    can be evaluated and is actually a condition. I would guess that the compiler gets rid of it when optimizing.

    Holger