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

Home Posts Topics Members FAQ

comparing unsigned long and unsigned int

#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type

What is happening here?
Thanks
sridhar
Nov 14 '05 #1
6 8070
In article <74************ **************@ posting.google. com>,
v_*******@mailc ity.com (sridhar) wrote:
#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type


The compiler is letting you know that 0ul <= ui is always true, since
unsigned numbers can't be negative.

Cheers,
- jonathan
Nov 14 '05 #2
v_*******@mailc ity.com (sridhar) writes:
#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type


You're not getting the warning because you're comparing ui to an
unsigned long; you're getting the warning because you're comparing ui
to zero.

Think about it. ui is unsigned. For what possible values of ui would
(0x0ul <= ui) or, more simply, (ui >= 0), be false?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
Keith Thompson <ks***@mib.or g> wrote in message news:<ln******* *****@nuthaus.m ib.org>...
v_*******@mailc ity.com (sridhar) writes:
#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type


You're not getting the warning because you're comparing ui to an
unsigned long; you're getting the warning because you're comparing ui
to zero.

Think about it. ui is unsigned. For what possible values of ui would
(0x0ul <= ui) or, more simply, (ui >= 0), be false?


Well I understood the fact that no value of ui can make this condition
false.
But when I replaced 0ul with 0u I didn't get the warning.When I
retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
didn't get the warning either. I was just trying to understand the
significance of ul being 64 bits to trigger this warning.I should've
mentioned this in the original post.
Nov 14 '05 #4
v_*******@mailc ity.com (sridhar) writes:
Keith Thompson <ks***@mib.or g> wrote in message
news:<ln******* *****@nuthaus.m ib.org>...
v_*******@mailc ity.com (sridhar) writes:
> #include <stdio.h>
> int main(){
> unsigned int ui = 0;
> if(0x0ul <= ui){
> printf("less eq\n");
> }
> }
>
> On my system unsigned long is 64 bits and unsigned int is 32.The
> compiler gives a warning
>
> warning: comparison is always true due to limited range of data type


You're not getting the warning because you're comparing ui to an
unsigned long; you're getting the warning because you're comparing ui
to zero.

Think about it. ui is unsigned. For what possible values of ui would
(0x0ul <= ui) or, more simply, (ui >= 0), be false?


Well I understood the fact that no value of ui can make this condition
false.
But when I replaced 0ul with 0u I didn't get the warning.When I
retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
didn't get the warning either. I was just trying to understand the
significance of ul being 64 bits to trigger this warning.I should've
mentioned this in the original post.


Unless I'm missing something obvious, it's probably just an odd quirk
of the compiler you're using. I can't think of any reason why
changing 0x0ul to 0x0u should inhibit the warning. (You used the same
command-line options in both cases, right?)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
In article <74************ **************@ posting.google. com>,
v_*******@mailc ity.com (sridhar) wrote:
Keith Thompson <ks***@mib.or g> wrote in message
news:<ln******* *****@nuthaus.m ib.org>...
v_*******@mailc ity.com (sridhar) writes:
#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type


You're not getting the warning because you're comparing ui to an
unsigned long; you're getting the warning because you're comparing ui
to zero.

Think about it. ui is unsigned. For what possible values of ui would
(0x0ul <= ui) or, more simply, (ui >= 0), be false?


Well I understood the fact that no value of ui can make this condition
false.
But when I replaced 0ul with 0u I didn't get the warning.When I
retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
didn't get the warning either. I was just trying to understand the
significance of ul being 64 bits to trigger this warning.I should've
mentioned this in the original post.


My explanation is a bit complicated: Obviously a comparison like "0x0ul
<= ui" is useless. It might indicate a bug in your program, and that is
when compilers are supposed to give warnings.

A typical example would be

unsigned int ui;
for (ui = 10; ui >= 0; --ui) f (ui);

This is an infinite loop, which might come as a surprise to the
programmer who wrote it, and it definitely should get a warning. On the
other hand, such useless comparisons are very often not a bug,
especially when the operands are constants. For example:

#define VERSION 3 /* Could be 1, 2, 3 or 4 */

if (VERSION == 1) {
...
} else if (VERSION == 2) {
...
} else if (VERSION == 3) {
...
} else if (VERSION == 4) {
...
}

After macro substitution, all the comparisons are "useless" but there is
no bug here. So it is quite likely that a compiler doesn't give warnings
when only constants are involved.

It could be that because of the mix of unsigned int and unsigned long in
your code the compiler doesn't recognize that only constants are
involved; it might not recognize "(unsigned long) ui" as a constant
anymore, and therefore you get a warning.

There are no exact rules for warnings, every compiler does its best to
give warnings on suspicious code and give no warnings on code that works
as intended, and getting this right is difficult.
Nov 14 '05 #6
v_*******@mailc ity.com (sridhar) wrote in message news:<74******* *************** ****@posting.go ogle.com>...
Keith Thompson <ks***@mib.or g> wrote in message news:<ln******* *****@nuthaus.m ib.org>...
v_*******@mailc ity.com (sridhar) writes:
#include <stdio.h>
int main(){
unsigned int ui = 0;
if(0x0ul <= ui){
printf("less eq\n");
}
}

On my system unsigned long is 64 bits and unsigned int is 32.The
compiler gives a warning

warning: comparison is always true due to limited range of data type


You're not getting the warning because you're comparing ui to an
unsigned long; you're getting the warning because you're comparing ui
to zero.

Think about it. ui is unsigned. For what possible values of ui would
(0x0ul <= ui) or, more simply, (ui >= 0), be false?


Well I understood the fact that no value of ui can make this condition
false.
But when I replaced 0ul with 0u I didn't get the warning.When I
retained ul and compiled it on a 32bit machine ( ul == 32bits ) I
didn't get the warning either. I was just trying to understand the
significance of ul being 64 bits to trigger this warning.I should've
mentioned this in the original post.


There is no significance in this case. It's a 'quality of implementation'
issue. It appears that when the compiler is "thinking about" the two
operands having a different size, it notices that the comparison is
always true and warns you about it. When they're the same size, it
doesn't notice (or doesn't choose to warn you).
Nov 14 '05 #7

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

Similar topics

7
3610
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
26
2057
by: Christopher Benson-Manica | last post by:
I have, in a certain unnamed header file, the following: #define PIPE_WAIT 0x00000000 #define PIPE_NOWAIT 0x00000001 and a function that the compiler believes has the following prototype: __stdcall SetNamedPipeHandleState(void *,unsigned long *, unsigned long *,unsigned long *); /* nonstandard calling convention */
6
4217
by: CFAN | last post by:
Here is a example of double-type-conversion char * __cdecl _itoa ( int val, char *buf, int radix ) { if (radix == 10 && val < 0) xtoa((unsigned long)val, buf, radix, 1); else
24
10461
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
5
2471
by: coleslaw01 | last post by:
Hello, I am trying to teach myself C++ while babysitting a stable network in Iraq and have put together a program to display the fibonacci sequence. It works with long and long double(output in exponent though) After sequence 47 using long type the program won't display the output properly (I am assuming because of size limit of type) so I switched type to unsigned long and now number 49 outputs as 512559680 when it should be 4807526976 ...
105
6242
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
0
9705
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
9576
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
10567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
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
10074
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...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.