SiteBanner

While 'C' programmers are often creative visionaries who craft elegant, functional code...  we are also - sadly - bug exterminators.   One source of bugs is when a variable's OVERFLOW or UNDERFLOW can give rise to aberrant program behavior.  Let's be clear on where the flow goes.

 

handpntright A new feature has been added to past & present articles, to download source code rather than use copy and paste into your text editor.  You'll need to use a different convention with 'goc' compile script:   "./goc pgm30_main.c  pgm30"

 


(right-click to download)

/* pgm30 source */

#include <stdio.h>

int main(void) {
     char ns_char;                     // no-sign, all char = 8 bits / 1 byte
     signed char s_char;           // signed -128 to 0 to +127
     unsigned char us_char;     // unsigned 0 to 255
     short ns_short;                  // no-sign, all short = 16 bits / 2 bytes
     signed short s_short;         // signed -32768 to +32767
     unsigned short us_short;   // unsigned 0 to 65535
     int ns_int;                           // no-sign, all int = 32 bits / 4 bytes
     signed int s_int;                 // signed -2147483648 to +2147483647
     unsigned int us_int;           // unsigned 0 to 4294967295

     printf("\n");

     ns_char = s_char = 127; us_char = 255;
     printf("--- 1 byte, 8 bits ---\n");
     printf("(upper limit) ns_char: %+d, s_char: %+d, us_char: %u\n", ns_char, s_char, us_char);

     ns_char++; s_char++; us_char++;
     printf("(increment) ns_char: %+d, s_char: %+d, us_char: %u\n", ns_char, s_char, us_char);

     ns_char++; s_char++; us_char++;
     printf("(increment) ns_char: %+d, s_char: %+d, us_char: %u\n\n", ns_char, s_char, us_char);

     ns_char = s_char = -128; us_char = 0;
     printf("(lower limit) ns_char: %+d, s_char: %+d, us_char: %u\n", ns_char, s_char, us_char);

     ns_char--; s_char--; us_char--;
     printf("(decrement) ns_char: %+d, s_char: %+d, us_char: %u\n", ns_char, s_char, us_char);

     ns_char--; s_char--; us_char--;
     printf("(decrement) ns_char: %+d, s_char: %+d, us_char: %u\n\n", ns_char, s_char, us_char);

     /* ******************** */

     ns_short = s_short = 32767; us_short = 65535;
     printf("--- 2 bytes, 16 bits ---\n");
     printf("(upper limit) ns_short: %+d, s_short: %+d, us_short: %u\n", ns_short, s_short, us_short);

     ns_short++; s_short++; us_short++;
     printf("(increment) ns_short: %+d, s_short: %+d, us_short: %u\n", ns_short, s_short, us_short);

     ns_short++; s_short++; us_short++;
     printf("(increment) ns_short: %+d, s_short: %+d, us_short: %u\n\n", ns_short, s_short, us_short);

     ns_short = s_short = -32768; us_short = 0;
     printf("(lower limit) ns_short: %+d, s_short: %+d, us_short: %u\n", ns_short, s_short, us_short);

     ns_short--; s_short--; us_short--;
     printf("(decrement) ns_short: %+d, s_short: %+d, us_short: %u\n", ns_short, s_short, us_short);

     ns_short--; s_short--; us_short--;
     printf("(decrement) ns_short: %+d, s_short: %+d, us_short: %u\n\n", ns_short, s_short, us_short);

     /* ******************** */

     ns_int = s_int = 2147483647; us_int = 4294967295;
     printf("--- 4 bytes, 32 bits ---\n");
     printf("(upper limit) ns_int: %+d, s_int: %+d, us_int: %u\n", ns_int, s_int, us_int);

     ns_int++; s_int++; us_int++;
     printf("(increment) ns_int: %+d, s_int: %+d, us_int: %u\n", ns_int, s_int, us_int);

     ns_int++; s_int++; us_int++;
     printf("(increment) ns_int: %+d, s_int: %+d, us_int: %u\n\n", ns_int, s_int, us_int);

     ns_int = s_int = -2147483648; us_int = 0;
     printf("(lower limit) ns_int: %+d, s_int: %+d, us_int: %u\n", ns_int, s_int, us_int);

     ns_int--; s_int--; us_int--;
     printf("(decrement) ns_int: %+d, s_int: %+d, us_int: %u\n", ns_int, s_int, us_int);

     ns_int--; s_int--; us_int--;
     printf("(decrement) ns_int: %+d, s_int: %+d, us_int: %u\n", ns_int, s_int, us_int);

     printf("\n");
     return(0);
     } 

 

overflow_underflow