unsigned short;

  • Thread starter Vijay Kumar R Zanvar
  • Start date
V

Vijay Kumar R Zanvar

Hi clc,

Please elaborate the warning:

F:\Vijay\C> type unsigned2.c

#include <stdio.h>
#include <stdlib.h>

int
main ( void )
{
unsigned short i = 0;
for ( i = -10; i <= -1; i++ )
printf ("%u\n", i);
return EXIT_SUCCESS;
}

F:\Vijay\C> gcc unsigned2.c -Wall
unsigned2.c: In function `main':
unsigned2.c:10: warning: comparison is always false due to limited range of
data type


Thank You.
 
J

Joona I Palaste

Vijay Kumar R Zanvar said:
Please elaborate the warning:
F:\Vijay\C> type unsigned2.c
#include <stdio.h>
#include <stdlib.h>
int
main ( void )
{
unsigned short i = 0;
for ( i = -10; i <= -1; i++ )
printf ("%u\n", i);
return EXIT_SUCCESS;
}
F:\Vijay\C> gcc unsigned2.c -Wall
unsigned2.c: In function `main':
unsigned2.c:10: warning: comparison is always false due to limited range of
data type

How do you expect an unsigned short to ever be less than -1?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
V

Vijay Kumar R Zanvar

Joona I Palaste said:
How do you expect an unsigned short to ever be less than -1?

I am confused here. Any promotions/conversions happen here?
Can 6.3.1.3#2 not be applied?
 
M

Martin Ambuhl

Vijay said:
Hi clc,

Please elaborate the warning:

F:\Vijay\C> type unsigned2.c

#include <stdio.h>
#include <stdlib.h>

int
main ( void )
{
unsigned short i = 0;
for ( i = -10; i <= -1; i++ )
printf ("%u\n", i);
return EXIT_SUCCESS;
}

F:\Vijay\C> gcc unsigned2.c -Wall
unsigned2.c: In function `main':
unsigned2.c:10: warning: comparison is always false due to limited range of
data type

All unsigned integers are 0 or positive. They can never be less than a
negative value, since they cannot be negative.
 
N

Nejat AYDIN

Vijay said:
I am confused here. Any promotions/conversions happen here?
Can 6.3.1.3#2 not be applied?

No, not here. But 6.3.1.8#1 applies:
[...]
Otherwise, if the type of the operand with signed integer type can represent
all of the values of the type of the operand with unsigned integer type, then
the operand with unsigned integer type is converted to the type of the
operand with signed integer type.

Since -1 is of type ``int'' and ``int'' can represent all of the values of the
type of ``unsigned short'' (in your implementation), the variable ``i'' is
converted to ``int''. So the expression ``i <= -1'' becomes always false
because ``i'' is always non-negative.

If all of the values of type ``unsigned short'' couldn't be represented by
the type ``int'', then the variable ``i'' and ``-1'' would be converted to
``unsigned int'' and the expression would be always true.
 
M

Martin Dickopp

Vijay Kumar R Zanvar said:
I am confused here. Any promotions/conversions happen here?
Can 6.3.1.3#2 not be applied?

`i' is promoted to either `int', or, if `int' cannot represent all
values of `unsigned short', to `unsigned int'. Either way, the promotion
doesn't change the value. Since it was nonnegative to start with, it is
also nonnegative after the promotion.

6.3.1.3#2 applies only if the value cannot be represented by new type,
but in this case, it can. (Note the word "otherwise" in the standard
text.)

Martin
 
D

Dan Pop

In said:
`i' is promoted to either `int', or, if `int' cannot represent all
values of `unsigned short', to `unsigned int'. Either way, the promotion
doesn't change the value. Since it was nonnegative to start with, it is
also nonnegative after the promotion.

However, if i was promoted to unsigned int, then -1 was also converted
to unsigned int, the result being UINT_MAX. In this case, i <= -1
becomes a definite possibility.

Dan
 
D

Dan Pop

In said:
All unsigned integers are 0 or positive. They can never be less than a ^^^^^^^^^^^^^^^^^^^^^
negative value, since they cannot be negative.

C is not mathematics. Make the type of i unsigned int instead of unsigned
short and i <= -1 *always* evaluates to true.

Dan
 
D

Dan Pop

In said:
Vijay said:
I am confused here. Any promotions/conversions happen here?
Can 6.3.1.3#2 not be applied?

No, not here. But 6.3.1.8#1 applies:
[...]
Otherwise, if the type of the operand with signed integer type can represent
all of the values of the type of the operand with unsigned integer type, then
the operand with unsigned integer type is converted to the type of the
operand with signed integer type.

Since -1 is of type ``int'' and ``int'' can represent all of the values of the
type of ``unsigned short'' (in your implementation), the variable ``i'' is
converted to ``int''. So the expression ``i <= -1'' becomes always false
because ``i'' is always non-negative.

Completely bogus analysis. It is the following text, from the same
chapter and verse that applies here.

Otherwise, the integer promotions are performed on both
operands. Then the following rules are applied to the
promoted operands: ^^^^^^
^^^^^^^^^^^^^^^^^
If both operands have the same type, then no further
conversion is needed.

On his implementation, i becomes int after the integer promotions, so
both operands have the same type.
If all of the values of type ``unsigned short'' couldn't be represented by
the type ``int'', then the variable ``i'' and ``-1'' would be converted to
``unsigned int'' and the expression would be always true.

This is true, but the order of conversions would be: i becomes unsigned
int after the integral promotions, while -1 remains signed int. So, the
following paragraph applies:

Otherwise, if the operand that has unsigned integer
type has rank greater or equal to the rank of the
type of the other operand, then the operand with
signed integer type is converted to the type of
the operand with unsigned integer type.

Dan
 
D

Dan Pop

In said:
#include <stdio.h>
#include <stdlib.h>

int
main ( void )
{
unsigned short i = 0;
for ( i = -10; i <= -1; i++ )
printf ("%u\n", i);
return EXIT_SUCCESS;
}

F:\Vijay\C> gcc unsigned2.c -Wall
unsigned2.c: In function `main':
unsigned2.c:10: warning: comparison is always false due to limited range of
data type

You've got by now the correct answers. I only want to point out that
the type unsigned short is probably the most "perverse" type from the C
type system. Leave its usage to the expert C programmers.

An error nobody mentioned was passing i to %u, although it's obvious that
i gets promoted to signed int on your implementation, while %u expects
an unsigned int.

Furthermore, until you become an experienced C programmer, avoid as much
as possible mixing signed and unsigned in the same expression. You'll
avoid many "surprises" this way.

Dan
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top