Skip to content

Instantly share code, notes, and snippets.

@KirinDave
Created October 29, 2009 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KirinDave/221929 to your computer and use it in GitHub Desktop.
Save KirinDave/221929 to your computer and use it in GitHub Desktop.
#include <stdio.h>
/*******************************************************
* SAMPLE COMPILATION AND OUTPUT
*
* % gcc -o overflow_example example.c ; ./overflow_example
* I just overflowed, I am now negative!
* Current Value is: -32768
* Last Value is: 32767I have looped back upon myself to 0!
*
*******************************************************/
int main(int argc, char *argv[]) {
short current = 1, last = 0; // Values which are 2 bytes long and range from -32768 to 32767
// (including 0), or "16-bit signed integers".
while(current != 0) {
current = current + 1;
if(current < last) {
// Current is less than last, but if we're adding how can that possibly happen?!
// In a word... "Signed Overflow"
printf("I just overflowed, I am now negative!\nCurrent Value is: %d\nLast Value is: %d\n",
current,
last);
}
last = current;
}
printf("I have looped back upon myself to 0!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment