rank and expressions

E

Emmanuel Delahaye

j0mbolar wrote on 30/07/04 :
what's the rules of rank and how they affect types of expressions?

What are you talking about ? Can you be more specific ?
 
J

j0mbolar

Emmanuel Delahaye said:
j0mbolar wrote on 30/07/04 :

What are you talking about ? Can you be more specific ?

I thought I was specific enough. rank only exist for one aspect
of C and that is types; like int <= unsigned int <= long
I don't know the rules exactly, which is why I was asking.

There are problems with this model iirc, because
int can be larger than unsigned int, so an expression
involving a variable that's type is unsigned int and a
variable that's type is int, the unsigned int variable
would be promoted to int, right?

and for variables of type unsigned short or unsigned char,
used with constants or variables of type int, get promoted
to type int or they can be promoted to type unsigned int
if int can't represent them. Though I think that would
never be the case as int would probably always be able
to represent unsigned char or unsigned short.

Also, I wonder if the case of short int not being able
to be represented by int could ever arise.
 
E

Eric Sosman

j0mbolar said:
I thought I was specific enough. rank only exist for one aspect
of C and that is types; like int <= unsigned int <= long
I don't know the rules exactly, which is why I was asking.

There are problems with this model iirc, because
int can be larger than unsigned int, so an expression
involving a variable that's type is unsigned int and a
variable that's type is int, the unsigned int variable
would be promoted to int, right?

No. All non-negative `int' values can be represented
as `unsigned int' (section 6.2.6.2 paragraph 2).
and for variables of type unsigned short or unsigned char,
used with constants or variables of type int, get promoted
to type int or they can be promoted to type unsigned int
if int can't represent them. Though I think that would
never be the case as int would probably always be able
to represent unsigned char or unsigned short.

Some people have written here about machines on which
`char', `short', and `int' are all 32 bits wide. On such
a machine, `unsigned char' and `unsigned short' can express
values that are too large for `int'.

I haven't personally used such machines, but I have
used systems on which `short' and `int' were both 16 bits
wide. On such systems, an `unsigned short' could hold
values too large for an `int'.
Also, I wonder if the case of short int not being able
to be represented by int could ever arise.

Every `short' value can be represented as an `int'
(section 6.2.5 paragraph 8, plus 6.3.1.1 paragraph 1).
More completely, every `signed char' value can be
represented as a `short', every `short' value can be
represented as an `int', and every `int' value can be
represented as a `long'. This is sometimes shown
informally as

signed char <= short <= int <= long

In C89 it was easy to enumerate all the signed and
unsigned integer types and arrange them in a "pecking
order" like this one. But C99 has a thornier problem
to solve, not because of the introduction of `long long'
and `_Bool' (it'd be easy to insert them in a C89-style
list) but because the C99 Standard permits an implementation
to define new integer types beyond those explicitly named.
Some of these types, if the implementation provides them,
are given special names in <stdint.h> (section 7.18.1). But
now you've got a problem: Does `int_least16_t' promote to
`int', to `unsigned int', or perhaps not at all? Well, it
all depends on the implementation-defined characteristics
of both `int' and `int_least16_t', doesn't it? How is the
Standard to speak sensibly about all the possible variations?

To cope with the problem, C99 introduces the notion of
the "rank" of an integer type. 6.3.1.1 describes how ranks
are determined, and it's really just the `signed char <= short'
chain you're familiar with from C89, with generalizations to
allow other implementation-defined types to be inserted in
the appropriate positions. The "rank" of an integer type is
simply its position in the pecking order, and the concept
permits the Standard to describe promotions and conversions
in a general way without trying to enumerate every possible
integer type every implementation might ever dream up.
 

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
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top