Placing suffix u with unsigned variables

Discussion in 'General Electronics Chat' started by aamirali, Feb 4, 2014.

  1. aamirali

    Thread Starter Member

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

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

    Code ( (Unknown Language)):
    1. uint8_t my_arr[10U];
    2.  
    2. Also while using switch cases like:

    Code ( (Unknown Language)):
    1. void func(uint8_t no)
    2. {
    3.     switch(no)
    4.     {
    5.          case 1U:
    6.          break;
    7.  
    8.          case 2U:
    9.          break;
    10.  
    11.     }
    12. }
    If yes then what if variable passed to switch is of int8_t type. In above example if

    Code ( (Unknown Language)):
    1. int8_t no;
     
    Last edited by a moderator: Feb 5, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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
    Code ( (Unknown Language)):
    1.  uint8_t x;
    2.  if (x == 1U)
    3.     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).
     
Loading...