8

When reading nginx source code, I find this line:

#define NGX_INT32_LEN   sizeof("-2147483648") - 1

why using sizeof("-2147483648") - 1?

not sizeof(-2147483648) - 1

not -2147483648 - 1

not -2147483649 or else?

What's the difference?

NStal
  • 959
  • 1
  • 8
  • 19
  • 2
    `sizeof("1234")` definitely isn't the same as `sizeof(1234)`, which definitely isn't the same as `1234`. The first is the size of a character array, the second is the size of an integer, and the third is the plain value. – chris Nov 19 '12 at 04:03

1 Answers1

12

Basically -2147483648 is the widest, in terms of characters required for its representation, of any of the signed 32-bit integers. This macro NGX_INT32_LEN defines how many characters long such an integer can be.

It does this by taking the amount of space needed for that string constant, and subtracting 1 (because sizeof will provided space for the trailing NUL character). It's quicker than using:

strlen("-2147483648")

because not all compilers will translate that into the constant 11.

Edmund
  • 10,533
  • 3
  • 39
  • 57