Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 998
NStal Avatar asked Nov 19 '12 03:11

NStal


1 Answers

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.

like image 180
Edmund Avatar answered Oct 21 '22 20:10

Edmund