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

Data type question --- unsigned int variable

I am using Uvision2. For some reasons
I still see the negative value when declared a variable as unsigned interger --
According to the compiler, it supposes to be positive number from 0 - 65536.

Here is the sample test code I have used:

unsigned int A = 0;
while (1)
 {
   printf("\n\r Value= %d", A);
   A++;
 }
Problems:
Value would start from 0 to 32768 and then
-32768 down to 0.

Could somebody in this forum tell me
what I did wrong here.

Thanks in advance,


JIMMY

Parents
  • As Dan says, the problem here is not the data type of 'A' - it is the way that you're asking printf to interpret it.

    Out of interest, you might like to try this:

    unsigned int A = 0;
    while (1)
    {
       printf("\n\r Unsigned Value= %u", A);
       printf("\n\r Signed   Value= %d", A);
       A++;
    }
    so you can see that the same 'A' is being used in both printf calls - only the interpetation is different!

    Note that this is standard 'C' behaviour - nothing specific to Keil.

Reply
  • As Dan says, the problem here is not the data type of 'A' - it is the way that you're asking printf to interpret it.

    Out of interest, you might like to try this:

    unsigned int A = 0;
    while (1)
    {
       printf("\n\r Unsigned Value= %u", A);
       printf("\n\r Signed   Value= %d", A);
       A++;
    }
    so you can see that the same 'A' is being used in both printf calls - only the interpetation is different!

    Note that this is standard 'C' behaviour - nothing specific to Keil.

Children