Placing suffix u with unsigned variables

Thread Starter

aamirali

Joined Feb 2, 2012
412
I was reading on misra website that for assigning 0 to unsigned variable, 'u' must be suffixed.

For example:
Rich (BB code):
uint8_t my_cnt = 0U;
1. Does that even on declaring array I should use this.Like

Rich (BB code):
uint8_t my_arr[10U];
2. Also while using switch cases like:

Rich (BB code):
void func(uint8_t no)
{
    switch(no)
    {
         case 1U:
         break;

         case 2U:
         break;

    }
}
If yes then what if variable passed to switch is of int8_t type. In above example if

Rich (BB code):
int8_t no;
 
Last edited by a moderator:

tshuck

Joined Oct 18, 2012
3,534
0 is a 0 is a 0. The U just tells the compiler that this number has to be unsigned. The compiler will determine what representation (and size) is required to match the implied number.

In other words, it isn't necessary to put something like
Rich (BB code):
 uint8_t x;
 if (x == 1U)
    break;
unless your compiler is trying to use a signed number where an unsigned is needed (which would be very rare - and almost always result in a warning).
 
Top