-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsigned_plus_signed.c
66 lines (59 loc) · 1.43 KB
/
unsigned_plus_signed.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
If a signed value plus an unsigned value, the signed value will be transformed as an unsigned value(if it is a negative one, it will become a huge positive unsigned value), and then do the plus operation
If we make the above sum "=" to a signed value, signed value will NOT be transformed
*/
#include <stdio.h>
int main(void)
{
unsigned int u32a = 6;
int s32b = -10;
int s32c;
unsigned int u32d;
/********************************* 1st test *******************************/
if(u32a+s32b > 0)
{
printf("Positive\n"); // Do this, sum = 4294967292
}
else
{
printf("Negative\n");
}
/********************************* 2nd test *******************************/
if(s32b+u32a > 0)
{
printf("Positive\n"); // Do this, sum = 4294967292
}
else
{
printf("Negative\n");
}
/********************************* 3rd test *******************************/
s32c = u32a + s32b;
if(s32c > 0)
{
printf("Positive\n");
}
else
{
printf("Negative\n"); // Do this, sum = -4
}
/********************************* 4th test *******************************/
u32d = u32a + s32b;
if(u32d > 0)
{
printf("Positive\n"); // Do this, sum = 4294967292
}
else
{
printf("Negative\n");
}
return 0;
}
/*
Result:
Positive
Positive
Negative
Positive
*/
/* End of file */