Search
PDF
TeX
Real Numbers and Memory
ODP
Program add.c:
#include <stdio.h> int main() { unsigned char a=150u, b=120u, c; unsigned short as=40000u, bs=30000u, cs; char ac=-100, bc=-80, cc; short sas=-20000, sbs=-14000, scs; c = a+b; cs=as+bs; cc=ac+bc; scs=sas+sbs; printf("c=%u cs=%u cc=%i scs=%i\n", c, cs, cc, scs); }
Program add_test_overflow.c:
#include <stdio.h> int main() { unsigned char a=150u, b=120u, c; unsigned short as=40000u, bs=30000u, cs; char ac=-100, bc=-80, cc; short sas=-20000, sbs=-14000, scs; if ( __builtin_add_overflow(a,b,&c)) { printf("Overflow detected a+b\n"); } else { printf("c=%u\n", c); } a=120u; if ( __builtin_add_overflow(a,b,&c)) { printf("Overflow detected a+b\n"); } else { printf("c=%u\n", c); } if ( __builtin_add_overflow(as,bs,&cs)) { printf("Overflow detected as+bs\n"); } else { printf("cs=%u\n", cs); } as=33333u; if ( __builtin_add_overflow(as,bs,&cs)) { printf("Overflow detected as+bs\n"); } else { printf("cs=%u\n", cs); } if ( __builtin_add_overflow(ac,bc,&cc)) { printf("Overflow detected ac+bc\n"); } else { printf("cc=%i\n", cc); } ac=-40; if ( __builtin_add_overflow(ac,bc,&cc)) { printf("Overflow detected ac+bc\n"); } else { printf("cc=%i\n", cc); } if ( __builtin_add_overflow(sas,sbs,&scs)) { printf("Overflow detected sas+sbs\n"); } else { printf("scs=%i\n", scs); } sas=-12000; if ( __builtin_add_overflow(sas,sbs,&scs)) { printf("Overflow detected sas+sbs\n"); } else { printf("scs=%i\n", scs); } }
Program store.c:
#include <stdio.h> int main() { unsigned char p[] = {0,0,0xa0,0xbf}; printf("%i = %u = %f\n", *(int*)p, *(unsigned int *)p, *(float *)p); }
Program little.c:
#include <stdio.h> int main() { unsigned char p[] = {0,0,0xa0,0xbf}; *(int*)p=10; printf("%02x,%02x,%02x,%02x\n", p[0],p[1],p[2],p[3]); }