3

What is the difference between Usage

#define CONSTANT_1 (256u)
#define CONSTANT_2 (0XFFFFu)

and

#define CONSTANT_1 (256)
#define CONSTANT_2 (0XFFFF)

when do I really need to add u and what problems we get into if not?

I am more interested in the example expressions where one usage can go wrong with other usage.

IrAM
  • 1,720
  • 5
  • 18
  • Does these answer your question? [Meaning of U suffix](https://stackoverflow.com/questions/4380691/meaning-of-u-suffix) [What does 'u' mean after a number?](https://stackoverflow.com/questions/9029974/what-does-u-mean-after-a-number) – Sprite Jan 06 '21 at 07:57
  • @Sprite This does not cover where one usage can go wrong compare to other one when used with other expression. I am more interested in the usages, do I always need `u` at the end of number? – IrAM Jan 06 '21 at 08:12

2 Answers2

3

The trailing u makes the constant have unsigned type. For the examples given, this is probably unnecessary and may have surprising consequences:

#include <stdio.h>

#define CONSTANT_1 (256u)

int main() {
    if (CONSTANT_1 > -1) {
        printf("expected this\n");
    } else {
        printf("but got this instead!\n");
    }
    return 0;
}

The reason for this surprising result is the comparison is performed using unsigned arithmetics, -1 being implicitly converted to unsigned int with value UINT_MAX. Enabling extra warnings will save the day on modern compilers (-Wall -Werror for gcc and clang).

256u has type unsigned int whereas 256 has type int. The other example is more subtle: 0xFFFFu has type unsigned int, and 0xFFFF has type int except on systems where int has just 16 bits where it has type unsigned int.

Some industry standards such as MISRA-C mandate such constant typing, a counterproductive recommendation in my humble opinion.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • `0xFFFF` can be `int` and `unsigned int` on 16-bit systems? – IrAM Jan 06 '21 at 09:25
  • @IrAM: I meant `0xFFFF` has type `unsigned int` on 16-bit systems just like `0xFFFFu`. Answer amended. – chqrlie Jan 06 '21 at 09:50
  • 1
    not only 16-bit systems. 8-bit systems or any platforms where int is a 16-bit type will have that behavior. – phuclv Jan 06 '21 at 09:54
  • @phuclv: I guess hardware people refer to the width of the data bus where software people refer to the width of the registers used for the `int` type. I updated the answer to remove the ambiguity. – chqrlie Jan 06 '21 at 09:57
1

The u indicates that the decimal constant is unsigned.

Without that, because the value fits in the range of signed integer, it'll be taken as a signed one.

Quoting C11, chapter 6.4.4.1, Integer constants

The type of an integer constant is the first of the corresponding list in which 
its value can be represented.

Suffix    Decimal Constant            Octal or Hexadecimal Constant
---------------------------------------------------------------------------
none      int                         int
          long int                    unsigned int
          long long int               long int
                                      unsigned long int
                                      long long int
                                      unsigned long long int
   
u or U    unsigned int                unsigned int
          unsigned long int           unsigned long int
          unsigned long long int      unsigned long long int
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I am aware it indicates `unsigned`, but I am more interested in their usage in expressions where one can go wrong with other. – IrAM Jan 06 '21 at 08:18