473,804 Members | 3,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4361
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.hel sinki.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.hel sinki.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.hel sinki.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.hel sinki.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.hel sinki.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**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.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%************ ******@newsread 3.news.atl.eart hlink.net> Martin Ambuhl <ma*****@earthl ink.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********@sup eronline.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
16694
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 - 3;
16
5154
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
20
5371
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 <stdlib.h>
4
15360
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 necessary to explicitly cast from one type of unsigned integer type to another even though they do so implicitly?
4
5214
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 'uint16_t short'. The compiler (GNU 3.3.5) allows this but ignores the original unsigned keyword and initialzes the variable as a signed short int. I created a very simple test program just to see what was happening: #include <stdio.h> ...
10
5653
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; } else
6
6462
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 vaguely . Can
2
4588
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 short *)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart() +2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart() +8))),htons(*((unsigned short...
28
19445
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 used as the sign bit for the 16 bit signed integer. Any ideas/help greatly appreciated. Thanks.
3
6411
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 function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.