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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-15-2014, 11:41 AM
|
#1
|
Member
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Rep:
|
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".
|
|
|
08-15-2014, 12:07 PM
|
#2
|
Senior Member
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982
|
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.
|
|
|
08-15-2014, 12:36 PM
|
#3
|
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:
|
How are we able to store negative int in "unsigned int" ?
Looks like it's stored in the same way as signed int.
|
|
|
08-15-2014, 01:04 PM
|
#4
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,956
|
Quote:
Originally Posted by barunparichha
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
|
|
|
08-15-2014, 01:43 PM
|
#5
|
Senior Member
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982
|
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.
|
|
|
08-15-2014, 01:54 PM
|
#6
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,956
|
Quote:
Originally Posted by metaschima
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.
|
|
|
08-15-2014, 01:57 PM
|
#7
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,956
|
Quote:
Originally Posted by metaschima
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.
|
|
|
08-15-2014, 04:05 PM
|
#8
|
Senior Member
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982
|
Quote:
Originally Posted by rtmistler
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.
|
|
|
08-15-2014, 04:19 PM
|
#9
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798
|
Quote:
Originally Posted by metaschima
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.
|
|
|
08-15-2014, 05:04 PM
|
#10
|
Senior Member
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982
|
oops, ok it does with -Wextra.
|
|
|
All times are GMT -5. The time now is 10:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|