Integral promotions doubt

P

Peter

As stated in 2003 standard:

"An rvalue of type char, signed char, unsigned char, short int, or
unsigned short
int can be converted to an rvalue of type int if int can represent all
the values of the source type; otherwise,
the source rvalue can be converted to an rvalue of type unsigned
int.".

When does the final part of the above rule apply? Since int is at
least as wide as all types listed above, I can't imagine a value which
could be represented in one of those types, but not in int type. I
haven't found an appropriate example in any book I've read so far. Can
you please give me an example showing that the above rule is not
redundant and there in fact are cases where conversion to int would be
wrong and conversion to unsigned int must be applied instead?
 
S

Stefan Ram

Peter said:
When does the final part of the above rule apply? Since int is at
least as wide as all types listed above, I can't imagine a value which
could be represented in one of those types, but not in int type.

When sizeof( int )= sizeof( unsigned char ), and CHAR_BITS=16
after #include <limits>, ( unsigned char )40000ul might not be
convertible to int.
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top