This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

signed character behavior

Other Parts Discussed in Thread: CCSTUDIO

Hi,

I'm using CCStudio V3.3  as compiler and my DSP is TMS320C672x. Below is my problem statement.

for ex,

#define MACRO 0x80

char a[5];

a[0] = 0x80;

and my condition is  " if((unsigned char)a[0] ==MACRO) "

how this statement is evaulated? As per my understanding about statement shall be executed as "true". But my code at times, it treats as "false".  What may be the cause for the inconsitance result?

PS: it was my mistake as i didnot declare it as unsigned character. Here my intention is to get clarify , why it is showing inconsistance results? and/or this type of behavior can we see any time? or anything to with my compiler?

Thanks

Sudhee

 

  • Hi Sudhee,

    What did you mean "at times" exactly?  The identical comparison '(unsigned char)a[0] == MACRO' of the code became true usually but sometime became false?  What version of compiler did you use?  Do you have a reproduction code?

    In general, from C language standard standpoint, my understanding is as following.

    In the code, MACRO is simply and literally substituted by 0x80 so the expression is treated as '(unsigned char)a[0] == 0x80)'.  C language standard defines the right side of '==' is simply treated as int.  On the other hand, the left side is unsigned char.  In the case, C language's "assignment conversion" applies and the left side is converted to 'int' (or signed int') and becomes signed integer 0x80 (or 128).  So the comparison always results TRUE.

    On the other hand, if the casting '(unsigned char)' to the a[0] is missing, the left side is treated as -128 (even 0x80).  In the case, the comparison results FALSE because -128 is NOT equal to 128.

    Best Regards,
    Atsushi

  • Sorry, one correction.

    I noticed C language definition said "type char may be signed or unsigned" and compiler are free to choose.  But I don't think it affects the conclusion above...

    Regards,
    Atsushi

  • Hi Atsushi,

    Thank you very much for the response. Here are my answer for queries,

    #1. Version of CCSstudio is 3.3.38.2

    #2. "At times" - Means the behavior is inconsistant. There are couple of instances where the statement evaluted as "True" but couple of instance are not, even though the values remained same. (The array is assigned to particular memory in DSP, so we read the memory and value comes out to be 0x80, so we are sure that variable had 0x80 when it evaluated as "false").

  • sudheendra MS said:
    As per my understanding about statement shall be executed as "true".

    Based on what I see here, I think you are correct.

    sudheendra MS said:
    But my code at times, it treats as "false". 

    To understand this, we need a test case which allows us to reproduce the behavior.  Please submit one as described in the last part of the forum guidelines.

    Thanks and regards,

    -George

  • HI sudheendra,

    As George mentioned, a reproduction test case is helpful.  Any C language expressions are compiled to assembly code.  If we have a reproduction case and compiler version, we can get assembly code and isolate a root cause by them.

    > #1. Version of CCSstudio is 3.3.38.2

    We are afraid that CCS version does not directly tell compiler version.  When you build a project. you see compiler version in the build message.  Could you check it?

    Regards,
    Atsushi

  • Yokoyama, Atsushi said:
    In the case, C language's "assignment conversion" applies

    For the equality operator, the conversions that apply are the "usual arithmetic conversions", which is I'm sure what you were thinking of.  I agree with your assessment that the comparison should always by true, and that we need a reproducible test case in order to perform further analysis.

  • Hi Archaeologist,

    Thank you for your correction.  Yes, I should have said usual arithmetic (or binary) conversion.  Sorry for my confusion.

    Regards,
    Atsushi