Skip to content

Instantly share code, notes, and snippets.

@elliottwilliams
Created September 3, 2014 03:23
Show Gist options
  • Save elliottwilliams/22b3a58e4cb80ae7fafd to your computer and use it in GitHub Desktop.
Save elliottwilliams/22b3a58e4cb80ae7fafd to your computer and use it in GitHub Desktop.
C overflow demonstration
λ ~/Desktop/ ./overflow
Before any reassignment:
i = 512 = 0x200
c = 127 = 0x7f
When i = c, then c = i:
i = 127 = 0x7f
c = 127 = 0x7f
When c = i, then i = c:
i = 0 = 0x0
c = 0 = 0x0
#include <stdio.h>
int main() {
int i = 512;
char c = 127;
printf("Before any reassignment: \n");
printf("i = %d \t= 0x%x\n", i, i);
printf("c = %d \t= 0x%x\n", c, c);
i = c;
c = i;
printf("When i = c, then c = i:\n");
printf("i = %d \t= 0x%x\n", i, i);
printf("c = %d \t= 0x%x\n", c, c);
i = 512;
c = 127;
c = i;
i = c;
printf("When c = i, then i = c:\n");
printf("i = %d \t= 0x%x\n", i, i);
printf("c = %d \t= 0x%x\n", c, c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment