473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4318
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
2
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
28
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.