LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-15-2014, 11:41 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Rep: Reputation: 32
Comparision between signed and unsigned int Linux C


Code:
#include<stdio.h>
int main(){
  signed int a=-1;
  unsigned int b=-1;
  if(a==b)
    printf("%d %d",a,b);
  else
    printf("Not equal");
  return 0;
}
In my linux box, it gives me "-1 -1", but was expecting "Not equal".
 
Old 08-15-2014, 12:07 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
#include <stdio.h>

int main(void)
{
  signed int a=-1;
  unsigned int b=-1;

  printf("%x %x\n", a, b);

  if(a==b)
  {
    printf("%d %d\n",a,b);
    printf("%u %u\n",a,b);
  }
  else
  {
    printf("Not equal\n");
  }

  return 0;
}
Code:
ffffffff ffffffff
-1 -1
4294967295 4294967295
As you can see '-1' is represented as 0xffffffff in both cases. Also, printf will print a variable however you want.
 
Old 08-15-2014, 12:36 PM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
How are we able to store negative int in "unsigned int" ?
Looks like it's stored in the same way as signed int.
 
Old 08-15-2014, 01:04 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by barunparichha View Post
How are we able to store negative int in "unsigned int" ?
Looks like it's stored in the same way as signed int.
Never make assumptions, that's why Metaschima wrote additional code to make a point. You should also determine what the size of an integer is:

Code:
printf("Size of an integter is: %d\n", sizeof(int));
You should also be cognizant that just because something compiles doesn't mean it's correct. The default compiler settings are near worthless:

Code:
~/testcode$ 
~/testcode$ gcc -Wextra -o signed signed.c 
signed.c: In function ‘main’:
signed.c:10:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
~/testcode$ 
~/testcode$ gcc -Wsign-compare -o signed signed.c 
signed.c: In function ‘main’:
signed.c:10:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
~/testcode$
Here are some suggestions which I use as part of a Makefile, in fact from this very forum I added one or two to my list of checks. The whole point here is that the art of C programming is not just writing the programs but also understanding how the compiler treats what you have written. That actually was a huge point made by a course instructor years and years ago. The class was being taught commercially to our company and it actually wasn't about C programming, but instead network programming in C, but we Engineers were so self entitled that we started code style fights with the instructor and for him fortunately he was a C master and he pretty much shut us down at every juncture by being 1000% correct. I've been doing C for 25 years and I'm not always correct. Good points to remember are that one about understanding how the compiler interprets what you wrote, to test your hypothesis, and code accordingly so that you're not making any assumptions.

Code:
CFLAGS= -I. -O0 -ggdb -Wformat=2 -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations 
-Wmissing-noreturn
 
Old 08-15-2014, 01:43 PM   #5
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Heh, none of those warning options produces an error in this case, at least not for my version of gcc.

It's best to do logical things in your programs. Assigning a negative number to an unsigned int is just not logical.

In the usual compiler representation a 4 byte unsigned int goes from 0x0 = 0 to 0xffffffff = 4294967295. A signed int is usually represented as going from 0x0 = 0 to 0x7fffffff = 2147483647, and the negative values 0xffffffff = -1 to 0x80000000 = -2147483648. This representation is not mandatory, so don't bet on it.

Last edited by metaschima; 08-15-2014 at 01:44 PM.
 
Old 08-15-2014, 01:54 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by metaschima View Post
Heh, none of those warning options produces an error in this case, at least not for my version of gcc.
Not even with -Werror? That's supposed to force a warning to be interpreted as an error and cause compilation to exit.
 
Old 08-15-2014, 01:57 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by metaschima View Post
It's best to do logical things in your programs. Assigning a negative number to an unsigned int is just not logical.
Yeah, I kinda get frustrated when someone decrements an unsigned number and it wraps to be 0xffffffff and they get offended that it grew to a very large value or the way they wrote code it got interpreted as negative and they're all angry about it. Really it's just their personal interpretation which doesn't match the compiler and they're offended that, that nasty compiler dared to do something unexpected.
 
Old 08-15-2014, 04:05 PM   #8
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by rtmistler View Post
Not even with -Werror? That's supposed to force a warning to be interpreted as an error and cause compilation to exit.
No warnings and no errors.
 
Old 08-15-2014, 04:19 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by metaschima View Post
No warnings and no errors.
Even with -Wextra ?
Code:
$ gcc -o int -Wextra int.c
int.c: In function 'main':
int.c:10:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(a==b)
       ^
$ gcc -v
...
gcc version 4.8.2 (GCC)

Last edited by keefaz; 08-15-2014 at 04:20 PM.
 
Old 08-15-2014, 05:04 PM   #10
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
oops, ok it does with -Wextra.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - printf with %d for unsigned char vs unsigned int average_user Programming 10 09-05-2013 01:45 PM
unsigned long int uint32_t to unsigned char and back MicahCarrick Programming 2 08-02-2009 01:39 AM
signed/unsigned int and vector::size() [KIA]aze Programming 1 11-23-2007 06:19 PM
Problem with sending a signed int to another signed int. Almost random number given. RHLinuxGUY Programming 8 08-15-2006 11:38 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration