Type of Constant

G

Girish Sharma

I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?

Thank you.

Girish Sharma
(remove remv_ from address)
 
J

Jack Klein

I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?

No, that expression can't be an int. The constant "-32768" actually
consists of two parts. One is the literal constant "32768". The
other is the unary minus operator.

The numeric literal 32768 has type signed long on implementations
where INT_MAX is 32767. The unary minus operator is then applied to
the signed long value 32767 resulting in the signed long value -32768.

This expression can be used to initialize or assign to an int value,
if INT_MIN is defined as -32768, and the result is well defined
because even though the expression has the type signed long, its value
is within the range of values for an int.
 
C

Christian Bau

Jack Klein said:
No, that expression can't be an int. The constant "-32768" actually
consists of two parts. One is the literal constant "32768". The
other is the unary minus operator.

The numeric literal 32768 has type signed long on implementations
where INT_MAX is 32767. The unary minus operator is then applied to
the signed long value 32767 resulting in the signed long value -32768.

This expression can be used to initialize or assign to an int value,
if INT_MIN is defined as -32768, and the result is well defined
because even though the expression has the type signed long, its value
is within the range of values for an int.

On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)
sizeof (-32768) == sizeof (long)
sizeof (INT_MIN) != sizeof (-32768)

so you will usually find

#define INT_MIN (-32767 - 1)
 
P

Peter Nilsson

Christian Bau said:
On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)

Does C90 (or C95) not require INT_MIN to be of type int, as C99 does? [The requirement
seems absent from the C89 draft.]
 
K

Keith Thompson

Girish Sharma said:
I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?

-32768 is not an integer constant; it's an expression consisting of an
integer constant 32768 with a unary "-" operator applied to it.
 
J

Jack Klein

On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)
sizeof (-32768) == sizeof (long)
sizeof (INT_MIN) != sizeof (-32768)

so you will usually find

#define INT_MIN (-32767 - 1)

I'm not exactly sure from what you posted whether you are agreeing
with me and providing supplemental material, or arguing with me.

I think you're agreeing with me...

Another way to define INT_MIN, and also handy if you are writing your
own <stdint.h> header for an older compiler that lacks one, is:

#define INT_MIN (-INT_MAX - 1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top