471,056 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,056 software developers and data experts.

unsigned short;

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.
--
Vijay Kumar R Zanvar
Nov 14 '05 #1
10 3832
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
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


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #2

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
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


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?
Nov 14 '05 #3
Vijay Kumar R Zanvar wrote:
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.
Nov 14 '05 #4
Vijay Kumar R Zanvar wrote:

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
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


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?


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.
Nov 14 '05 #5
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
> 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


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?


`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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #6
In <cu*************@zero-based.org> Martin Dickopp <ex****************@zero-based.org> writes:
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
> 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

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?


`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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
In <c1**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Vijay Kumar R Zanvar <vi*****@hotpop.com> scribbled the following:
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


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


It is a perfectly possible scenario. Can you figure out when?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
In <Z%******************@newsread3.news.atl.earthlink .net> Martin Ambuhl <ma*****@earthlink.net> writes:
Vijay Kumar R Zanvar wrote:
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.


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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
In <40***************@superonline.com> Nejat AYDIN <ne********@superonline.com> writes:
Vijay Kumar R Zanvar wrote:

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
In <c1*************@ID-203837.news.uni-berlin.de> "Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
#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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by techie | last post: by
4 posts views Thread by slougheed | last post: by
10 posts views Thread by Jim Langston | last post: by
6 posts views Thread by Kislay | last post: by
2 posts views Thread by sam.barker0 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.