What does 'u' mean after a number?

前端 未结 4 1730
情话喂你
情话喂你 2020-12-28 12:54

Can you tell me what exactly does the \'u\' after a number, for example:

#define NAME_DEFINE 1u 
相关标签:
4条回答
  • 2020-12-28 13:33

    It is a way to define unsigned literal integer constants.

    0 讨论(0)
  • 2020-12-28 13:33

    it means "unsigned int", basically it functions like a cast to make sure that numeric constants are converted to the appropriate type at compile-time.

    0 讨论(0)
  • 2020-12-28 13:56

    Integer literals like 1 in C code are always of the type int. int is the same thing as signed int. One adds u or U (equivalent) to the literal to ensure it is unsigned int, to prevent various unexpected bugs and strange behavior.

    One example of such a bug:

    On a 16-bit machine where int is 16 bits, this expression will result in a negative value:

    long x = 30000 + 30000;
    

    Both 30000 literals are int, and since both operands are int, the result will be int. A 16-bit signed int can only contain values up to 32767, so it will overflow. x will get a strange, negative value because of this, rather than 60000 as expected.

    The code

    long x = 30000u + 30000u;
    

    will however behave as expected.

    0 讨论(0)
  • 2020-12-28 14:00

    It is a way of telling the compiler that the constant 1 is meant to be used as an unsigned integer. Some compilers assume that any number without a suffix like 'u' is of int type. To avoid this confusion, it is recommended to use a suffix like 'u' when using a constant as an unsigned integer. Other similar suffixes also exist. For example, for float 'f' is used.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题