IrAM
IrAM

Reputation: 1738

macro constants with and without "u" at the end of a number

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.

Upvotes: 3

Views: 1183

Answers (2)

chqrlie
chqrlie

Reputation: 145297

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.

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

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

Upvotes: 1

Related Questions