Created
October 29, 2009 23:13
-
-
Save KirinDave/221929 to your computer and use it in GitHub Desktop.
This file contains 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> | |
/******************************************************* | |
* 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