By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

rank and expressions

P: n/a
what's the rules of rank and how they affect types of expressions?
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

P: n/a
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote in message news:<mn***********************@YOURBRAnoos.fr>...
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 ?


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.
Nov 14 '05 #3

P: n/a
j0mbolar wrote:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote in message news:<mn***********************@YOURBRAnoos.fr>...
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 ?

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.

--
Er*********@sun.com

Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.