Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone. i have got some problem using unsigned ints in c language.
when the code below runs it outputs 1 which means it is true. is -2 bigger than 5?

What I have tried:

C++
#include <stdio.h>
#include <math.h>

int main() {

int a=-3 ;

unsigned int b=5 ;

printf("%d",a>b) ;

return 0;
}
Posted
Updated 14-Oct-19 22:46pm
v2
Comments
Richard MacCutchan 14-Oct-19 11:03am    
You should not compare signed and unsigned values, as they are (logically) different types. A bit like comparing sheep with goats.

It is converting the int to an unsigned int and then doing the comparison. This int = -3, which is 0xfffffffd (32 bit) which when viewed as an unsigned it is 4294967293, which is larger than 5 and hence the comparison is true.
 
Share this answer
 
Comments
CPallini 15-Oct-19 4:40am    
5.
jeron1 15-Oct-19 10:02am    
Thanks :-)
While Jeron1 and F-ES Sitcore (nearly) are right, I thought perhaps some background might help you understand why they are different.
Integers come in several "sizes", which are generally related to the size of a machine word: In C that normally means 8 bits, 16 bits (old, but still used), 24 bits (occasionally), 32 bits (normal), 64 bits (often called "long), and 128 bits(specialist). There are probably bigger ones, but I've not met them.
The number of bits in the integer determines how many different values it can hold:
Bits   Number of different values
  8                                                    256
 16                                                 65,536
 32                                          4,294,967,296
 64                             18,446,744,073,709,551,616
128    340,282,366,920,938,463,463,374,607,431,768,211,456

The problem is that if you want integers, you normally want positive and negative numbers as well - it's unusual to restrict yourself to just positive values.
So you tend to end up with two different "flavours" of integer: signed and unsigned, and the number of bits in the number doesn't change, so the number of different values doesn't change. and that means that unsigned integers can hold a "bigger number" of positive values than signed integers, but signed integers can hold the same number of positive and negative numbers*, while unsigned integers can't hold any negative numbers at all.

They do this by saying "if the top (most significant) bit of teh number is one, the number is negative."

For example, let's look at 4 bits integers just to make it easier.
      signed                        unsigned
0000  0                              0
0001  1                              1
0010  2                              2
0011  3                              3
0100  4                              4
0101  5                              5
0110  6                              6
0111  7                              7
1000 -8 (or "negative zero")         8
1001 -7                              9
1010 -6                             10
1011 -5                             11
1100 -4                             12
1101 -3                             13
1110 -2                             14
1111 -1                             15
So if you have zero in a signed 4 bit integer, and you subtract one, you get 1111 (which is what you'd expect in binary math, the only one computers play with) representing -1.
Subtract another one and you get 1110 or -2
Add 6 and you get 0100 or 4.

It's important to note that in binary this is the same for signed and unsigned values: unsigned 0000 less one is still 1111, unsigned 1111 less one is still 1110, unsigned 1110 plus six is still 0100 - it's just the representation changes when the value is used and "converted" to a human readable form to present on a display. The internal math is still binary, the internal values are still just 4 bit (or 8, 16, 32, ...)


* Well, sort of. Technically zero is neither positive or negative, so you can consider it as one more negative number than positive. Unless your machine / language accepts the concept of "negative zero[^]" as a distinct entity, but that's pretty rare these days.
 
Share this answer
 
Comments
CPallini 15-Oct-19 4:40am    
5.
CodeWraith 15-Oct-19 5:40am    
Nah, only 4 for not mentioning 'one's complement' or 'two's complement' in the lecture and only hinting at the difference between those two.
OriginalGriff 15-Oct-19 5:49am    
:grin:
Implicit type promotion rules (is what happens in your a>b expression) is one of the trickiest corners of the C programming language. Read, for instance c - Implicit type promotion rules - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900