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 10 4242
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
"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?
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.
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.
"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. \_/
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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. ...
|
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...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |