Last active
July 30, 2017 05:18
-
-
Save odeblic/67509d57b1e2f73938eb248eeff68511 to your computer and use it in GitHub Desktop.
Signed and unsigned integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char* argv[]) | |
{ | |
union | |
{ | |
signed short int signed_short_int; | |
unsigned short int unsigned_short_int; | |
struct {unsigned char char1; unsigned char char2;} s; | |
} u; | |
signed short int a; | |
unsigned short int b; | |
a = -1; | |
b = (unsigned short int)a; | |
u.signed_short_int = a; | |
puts("binary arrangement:"); | |
printf("sshort\t= %d\n", u.signed_short_int); | |
printf("ushort\t= %d\n", u.unsigned_short_int); | |
printf("byte 0\t= %d\n", u.s.char1); | |
printf("byte 1\t= %d\n", u.s.char2); | |
printf("a\t= %d\n", a); | |
printf("b\t= %d\n", b); | |
int i; | |
puts("signed overflow:"); | |
a = 32765; | |
for (i=0; i<5; i++) | |
{ | |
printf("++a\t= %d\n", ++a); | |
} | |
puts("unsigned overflow:"); | |
b = 65533; | |
for (i=0; i<5; i++) | |
{ | |
printf("++b\t= %d\n", ++b); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment