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

Placing suffix u with unsigned variables

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

For example:

uint8_t my_cnt = 0U;

1. Does that even on declaring array I should use this.Like

uint8_t my_arr[10U];

2. Also while using switch cases like:

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

int8_t no;

Parents
  • Taken from document "MISRA-C:2004 Guidelines for the use of the C language in critical systems"

    
    Rule 10.6 (required): A “U” suffix shall be applied to all constants of unsigned type.
    
    The type of an integer constant is a potential source of confusion, because it is dependent on a
    complex combination of factors including:
    
      The magnitude of the constant
      The implemented sizes of the integer types
      The presence of any suffixes
      The number base in which the value is expressed (i.e. decimal, octal or hexadecimal).
    
    

    and

    
    Signedness of constants should be explicit. Consistent signedness is an important principle
    in constructing well formed expressions. If a constant is of an unsigned type, it is helpful
    to avoid ambiguity by applying a “U” suffix. When applied to larger values, the suffix may
    be redundant (in the sense that it does not influence the type of the constant); however
    its presence is a valuable contribution towards clarity.
    
    

    I never liked the look of Misra anyway.

Reply
  • Taken from document "MISRA-C:2004 Guidelines for the use of the C language in critical systems"

    
    Rule 10.6 (required): A “U” suffix shall be applied to all constants of unsigned type.
    
    The type of an integer constant is a potential source of confusion, because it is dependent on a
    complex combination of factors including:
    
      The magnitude of the constant
      The implemented sizes of the integer types
      The presence of any suffixes
      The number base in which the value is expressed (i.e. decimal, octal or hexadecimal).
    
    

    and

    
    Signedness of constants should be explicit. Consistent signedness is an important principle
    in constructing well formed expressions. If a constant is of an unsigned type, it is helpful
    to avoid ambiguity by applying a “U” suffix. When applied to larger values, the suffix may
    be redundant (in the sense that it does not influence the type of the constant); however
    its presence is a valuable contribution towards clarity.
    
    

    I never liked the look of Misra anyway.

Children
No data