tag:blogger.com,1999:blog-5592542.post4905764076009350036..comments2024-03-18T17:28:44.693-07:00Comments on Rondam Ramblings: Geek corner: A C puzzleRonhttp://www.blogger.com/profile/11752242624438232184noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-5592542.post-18860834749571511032011-07-25T05:36:18.331-07:002011-07-25T05:36:18.331-07:00This is a problem related to (I would say 'inv...This is a problem related to (I would say 'invisible') conversions. In this case, for x > i, its converting the signed int to unsigned int. <br /><br />I would say I only know its related to such magic tricks which I would never learn.<br /><br />Refer to C99 standard, I go to sleep!<br /><br />******<br /><br />Now as a corollary, I programmed in C, C++ for first 2 years and thought IDeepak Surtihttp://deepaksurti.comnoreply@blogger.comtag:blogger.com,1999:blog-5592542.post-32779198491118857382011-07-21T15:30:28.182-07:002011-07-21T15:30:28.182-07:00the -Wextra flag gets the warning for me:
MYDEV:~...the -Wextra flag gets the warning for me:<br /><br />MYDEV:~$ gcc -Wall -Wextra test.c<br />test.c: In function ‘main’:<br />test.c:7: warning: comparison between signed and unsigned integer expressions<br />test.c:7: warning: comparison between signed and unsigned integer expressionsUnknownhttps://www.blogger.com/profile/13237356805173393114noreply@blogger.comtag:blogger.com,1999:blog-5592542.post-55774341791462383692011-07-21T09:26:12.170-07:002011-07-21T09:26:12.170-07:00Hopefully, all of them :) You're not supposed...Hopefully, all of them :) You're not supposed to compare signed and unsigned integers.less is morenoreply@blogger.comtag:blogger.com,1999:blog-5592542.post-16787635300857256742011-07-21T00:31:10.003-07:002011-07-21T00:31:10.003-07:00@miles just out of curiosity, what compiler issues...@miles just out of curiosity, what compiler issues that warning?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5592542.post-10307609782384325592011-07-20T05:49:59.150-07:002011-07-20T05:49:59.150-07:00The signed int is converted to unsigned int so (x&...The signed int is converted to unsigned int so (x>i) is true because 11111111 > 00000000 (to however many bits).<br /><br />However, the unsigned short is converted to signed int so (x>s) is false because -1 < 0.<br /><br />There is a sequence of rules to be followed and they make sense altogether.less is morenoreply@blogger.comtag:blogger.com,1999:blog-5592542.post-22031274211488651682011-07-20T02:24:43.591-07:002011-07-20T02:24:43.591-07:00You must not be using gcc:
[ron@mighty:~]$ cat fo...You must not be using gcc:<br /><br />[ron@mighty:~]$ cat foo.c<br />#include <br /><br />int main() {<br /> unsigned int i = 0;<br /> unsigned short s = 0;<br /> int x = -1;<br /> printf("%d %d %d %d\n", x>i, x>s, i<x, s<x);<br /> return 0;<br />}<br />[ron@mighty:~]$ gcc -Wall foo.c<br />[ron@mighty:~]$Ronhttps://www.blogger.com/profile/11752242624438232184noreply@blogger.comtag:blogger.com,1999:blog-5592542.post-35830168481283933122011-07-20T01:32:33.319-07:002011-07-20T01:32:33.319-07:00So I decided to test this, and in C/C++'s defe...So I decided to test this, and in C/C++'s defense, I did get a 'signed/unsigned mismatch' warning when compiling it. <br /><br />Not quite sure I understand the behaviour though, will have to think about this one.Mileshttps://www.blogger.com/profile/04199527461309714703noreply@blogger.comtag:blogger.com,1999:blog-5592542.post-23843781864159651882011-07-20T01:15:06.129-07:002011-07-20T01:15:06.129-07:00No directly related, but I had to think of this fr...No directly related, but I had to think of this from the "things that can kill ya" department:<br />From C++ 03, 5.8/1: The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.<br /><br />http://blogs.msdn.com/b/larryosterman/archive/2011/02/11/the-case-of-the-inconsistent-right-shift-results.aspxTony Machhttps://www.blogger.com/profile/14823430729798784689noreply@blogger.comtag:blogger.com,1999:blog-5592542.post-18632381856673909822011-07-20T00:29:12.082-07:002011-07-20T00:29:12.082-07:00Or am I looking at it the wrong way, and the int i...Or am I looking at it the wrong way, and the int is being changed to an unsigned int?Mileshttps://www.blogger.com/profile/04199527461309714703noreply@blogger.comtag:blogger.com,1999:blog-5592542.post-68244468115328320242011-07-20T00:24:48.680-07:002011-07-20T00:24:48.680-07:00I think John Carmack mentioned this on his twitter...I think John Carmack mentioned this on his twitter feed the other day. Isn't the trick that an unsigned short is automatically changed to be an unsigned int as well or something whacky like that?Mileshttps://www.blogger.com/profile/04199527461309714703noreply@blogger.com