Armen Tsirunyan
Armen Tsirunyan

Reputation: 133122

Is -5 an integer literal?

Is -5 an integer literal? Or is 5 a literal, and -5 is an expression with unary minus taking a literal as an argument? The question arose when I was wondering how to hardcode smallest signed integer values.

Upvotes: 10

Views: 543

Answers (2)

Michael Burr
Michael Burr

Reputation: 340506

As Jerry Coffin said, the minus sign is not part of the literal. As for how to solve your ultimate question,

I was wondering how to hardcode smallest signed integer values

That's what INT_MIN (and the like in limits.h or stdint.h or wherever) is for.

If you look at how INT_MIN is defined, it'll probably look something like (-2147483647 - 1) to work around the problem raised by the question.

Upvotes: 3

Jerry Coffin
Jerry Coffin

Reputation: 490713

It's a unary minus followed by 5 as an integer literal. Yes, that makes it somewhat difficult to represent the smallest possible integer in twos complement.

Upvotes: 9

Related Questions