NStal
NStal

Reputation: 999

why sizeof("-2147483648") - 1

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?

Upvotes: 8

Views: 439

Answers (1)

Edmund
Edmund

Reputation: 10829

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.

Upvotes: 10

Related Questions