#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 3_11a.c 3_11a.cmp 3_11a_b.c 3_11ab.cmp 3_11atst.c 3_11b.c 3_11b.cmp 3_11c.c 3_11c.cmp 3_11d.c 3_11d.cmp 3_11dtst.c 3_11e.c 3_11e.cmp 3_11e1.c 3_11e2.c 3_11e3.c 3_11e4.c makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 22:57:51 EDT 1990 # echo x - 3_11a.c sed 's/^X//' > 3_11a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Convert a string of digits into an integer. // Version 1 #include #include int atoi(const char *s) { for (int i = 0; isdigit(*s); s++) i = i * 10 + (*s - '0'); if (*s) error("malformed integer"); return i; } !EOF! ls -l 3_11a.c echo x - 3_11a.cmp sed 's/^X//' > 3_11a.cmp << '!EOF!' *xp='', i=0, 0x0, 00 *xp='0', i=0, 0x0, 00 *xp='10', i=10, 0xa, 012 *xp='100', i=100, 0x64, 0144 *xp='2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='2147483648', i=-(, 0x80000000, 020000000000 *xp='123abc', error: malformed integer !EOF! ls -l 3_11a.cmp echo x - 3_11a_b.c sed 's/^X//' > 3_11a_b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Convert a string of digits into an integer, // checking for overflow // Version 2 #include #include #include int atoi(const char *s) { for (int i = 0; isdigit(*s); s++) { if (i > INT_MAX / 10) break; i *= 10; int n = *s - '0'; if (i > INT_MAX - n) break; i += n; } if (isdigit(*s)) error("integer overflow"); else if (*s) error("malformed integer"); return i; } !EOF! ls -l 3_11a_b.c echo x - 3_11ab.cmp sed 's/^X//' > 3_11ab.cmp << '!EOF!' *xp='', i=0, 0x0, 00 *xp='0', i=0, 0x0, 00 *xp='10', i=10, 0xa, 012 *xp='100', i=100, 0x64, 0144 *xp='2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='2147483648', error: integer overflow *xp='123abc', error: malformed integer !EOF! ls -l 3_11ab.cmp echo x - 3_11atst.c sed 's/^X//' > 3_11atst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include #ifdef TSTA #include "3_11a.c" #endif #ifdef TSTAB #include "3_11a_b.c" #endif #ifdef TSTB #include "3_11b.c" #endif #ifdef TSTC #include "3_11c.c" #endif char *x[] = { "", "0", "10", "100", "2147483647", "2147483648", "123abc", #if TSTB || TSTC "+0", "+10", "+100", "+2147483647", "+2147483648", "+123abc", "-0", "-10", "-100", "-2147483647", "-2147483648", "-123abc", "0x0", "0x10", "0x100", "0x2147483647", "0x2147483648", "0x123abc", "+0x0", "+0x10", "+0x100", "+0x2147483647", "+0x2147483648", "+0x123abc", "-0x0", "-0x10", "-0x100", "-0x2147483647", "-0x2147483648", "-0x123abc", "0xFfFfFf", "0xFfFfFfFf", "0x7FfFffff", "0x8000000", "0x7g", "+0xFfFfFf", "+0xFfFfFfFf", "+0x7fffffff", "+0x8000000", "+0x7g", "-0xFfFfFf", "-0xFfFfFfFf", "-0x7fffffff", "-0x8000000", "-0x7g", "00", "010", "0100", "017777777777", "037777777777", "020000000000", "+00","+010", "+0100", "+017777777777", "+037777777777", "+020000000000", "-00","-010", "-0100", "-017777777777", "-037777777777", "-020000000000", "017777777776", "017777777778", "019a", "+017777777776", "+017777777778", "+019a", "-017777777776", "-017777777778", "-019a", #endif #ifdef TSTC "\\344", "\\x344", "\\b", "\\f", "\\n", "\\r", "\\t", "\\v", "\\\\", "\\'", "\\\"", "\\0", #endif 0 }; jmp_buf jmpenv; void error(const char *x, ...) { cout << "error: " << x << "\n"; longjmp(jmpenv, 1); } main() { for (char **xp = x; *xp; xp++) if (setjmp(jmpenv)) ; // error occurred else { cout << "*xp='" << *xp << "', "; int i = atoi(*xp); cout << "i=" << i << ", 0x" << hex(i) << ", 0" << oct(i) << "\n"; } return 0; } !EOF! ls -l 3_11atst.c echo x - 3_11b.c sed 's/^X//' > 3_11b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Convert a string of signed digits into an integer. // Hex and octal prefixes are allowed. // Version 2 #define NO_ISODIGIT /* DELETE */ #include #include #include // convert hexadecimal 0-9, a-f, A-F // to its numeric value inline int hexvalue(char c) { if (isdigit(c)) return c - '0'; else if (c >= 'a' && c <= 'f') return c - 'a' + 10; else /* (c >= 'A' && c <= 'F') */ return c - 'A' + 10; } // return true if c is an octal digit inline int isodigit(char c) { return isdigit(c) && c < '8'; } #include "3_11e1.c" /* EXPAND defines TWOSCOMPLEMENT */ int atoi(const char *s) { char sign = 0; int i = 0; switch (*s) // remember the sign { case '+': case '-': sign = *s++; break; } // for positive numbers on a 2's complement machine, // the minimum # is -INT_MAX, not INT_MIN const int int_min = (TWOSCOMPLEMENT && (sign != '-')) ? (-INT_MAX) : INT_MIN; switch (*s) { case '0': // hex or octal switch (*++s) { case 'x': // hex number case 'X': for (s++; isxdigit(*s); s++) { if (-i > INT_MAX / 16) break; i *= 16; int n = hexvalue(*s); if (i < int_min + n) break; i -= n; } if (isxdigit(*s)) error("integer overflow"); break; default: // octal number for ( ; isodigit(*s); s++) { if (-i > INT_MAX / 8) break; i *= 8; int n = *s - '0'; if (i < int_min + n) break; i -= n; } if (isodigit(*s)) error("integer overflow"); break; } break; default: // decimal number for ( ; isdigit(*s); s++) { if (-i > INT_MAX / 10) break; i *= 10; int n = *s - '0'; if (i < int_min + n) break; i -= n; } if (isdigit(*s)) error("integer overflow"); break; } if (*s) error("malformed integer"); return sign == '-' ? i : -i; } !EOF! ls -l 3_11b.c echo x - 3_11b.cmp sed 's/^X//' > 3_11b.cmp << '!EOF!' *xp='', i=0, 0x0, 00 *xp='0', i=0, 0x0, 00 *xp='10', i=10, 0xa, 012 *xp='100', i=100, 0x64, 0144 *xp='2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='2147483648', error: integer overflow *xp='123abc', error: malformed integer *xp='+0', i=0, 0x0, 00 *xp='+10', i=10, 0xa, 012 *xp='+100', i=100, 0x64, 0144 *xp='+2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='+2147483648', error: integer overflow *xp='+123abc', error: malformed integer *xp='-0', i=0, 0x0, 00 *xp='-10', i=-10, 0xfffffff6, 037777777766 *xp='-100', i=-100, 0xffffff9c, 037777777634 *xp='-2147483647', i=-2147483647, 0x80000001, 020000000001 *xp='-2147483648', i=-(, 0x80000000, 020000000000 *xp='-123abc', error: malformed integer *xp='0x0', i=0, 0x0, 00 *xp='0x10', i=16, 0x10, 020 *xp='0x100', i=256, 0x100, 0400 *xp='0x2147483647', error: integer overflow *xp='0x2147483648', error: integer overflow *xp='0x123abc', i=1194684, 0x123abc, 04435274 *xp='+0x0', i=0, 0x0, 00 *xp='+0x10', i=16, 0x10, 020 *xp='+0x100', i=256, 0x100, 0400 *xp='+0x2147483647', error: integer overflow *xp='+0x2147483648', error: integer overflow *xp='+0x123abc', i=1194684, 0x123abc, 04435274 *xp='-0x0', i=0, 0x0, 00 *xp='-0x10', i=-16, 0xfffffff0, 037777777760 *xp='-0x100', i=-256, 0xffffff00, 037777777400 *xp='-0x2147483647', error: integer overflow *xp='-0x2147483648', error: integer overflow *xp='-0x123abc', i=-1194684, 0xffedc544, 037773342504 *xp='0xFfFfFf', i=16777215, 0xffffff, 077777777 *xp='0xFfFfFfFf', error: integer overflow *xp='0x7FfFffff', i=2147483647, 0x7fffffff, 017777777777 *xp='0x8000000', i=134217728, 0x8000000, 01000000000 *xp='0x7g', error: malformed integer *xp='+0xFfFfFf', i=16777215, 0xffffff, 077777777 *xp='+0xFfFfFfFf', error: integer overflow *xp='+0x7fffffff', i=2147483647, 0x7fffffff, 017777777777 *xp='+0x8000000', i=134217728, 0x8000000, 01000000000 *xp='+0x7g', error: malformed integer *xp='-0xFfFfFf', i=-16777215, 0xff000001, 037700000001 *xp='-0xFfFfFfFf', error: integer overflow *xp='-0x7fffffff', i=-2147483647, 0x80000001, 020000000001 *xp='-0x8000000', i=-134217728, 0xf8000000, 037000000000 *xp='-0x7g', error: malformed integer *xp='00', i=0, 0x0, 00 *xp='010', i=8, 0x8, 010 *xp='0100', i=64, 0x40, 0100 *xp='017777777777', i=2147483647, 0x7fffffff, 017777777777 *xp='037777777777', error: integer overflow *xp='020000000000', error: integer overflow *xp='+00', i=0, 0x0, 00 *xp='+010', i=8, 0x8, 010 *xp='+0100', i=64, 0x40, 0100 *xp='+017777777777', i=2147483647, 0x7fffffff, 017777777777 *xp='+037777777777', error: integer overflow *xp='+020000000000', error: integer overflow *xp='-00', i=0, 0x0, 00 *xp='-010', i=-8, 0xfffffff8, 037777777770 *xp='-0100', i=-64, 0xffffffc0, 037777777700 *xp='-017777777777', i=-2147483647, 0x80000001, 020000000001 *xp='-037777777777', error: integer overflow *xp='-020000000000', error: integer overflow *xp='017777777776', i=2147483646, 0x7ffffffe, 017777777776 *xp='017777777778', error: malformed integer *xp='019a', error: malformed integer *xp='+017777777776', i=2147483646, 0x7ffffffe, 017777777776 *xp='+017777777778', error: malformed integer *xp='+019a', error: malformed integer *xp='-017777777776', i=-2147483646, 0x80000002, 020000000002 *xp='-017777777778', error: malformed integer *xp='-019a', error: malformed integer !EOF! ls -l 3_11b.cmp echo x - 3_11c.c sed 's/^X//' > 3_11c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Convert a string of signed digits into an integer. // Hex and octal prefixes are allowed, as well as // character constants. // Version 3 #define NO_ISODIGIT /* DELETE */ #include #include #include // convert hexadecimal 0-9, a-f, A-F // to its numeric value inline int hexvalue(char c) { if (isdigit(c)) return c - '0'; else if (c >= 'a' && c <= 'f') return c - 'a' + 10; else /* (c >= 'A' && c <= 'F') */ return c - 'A' + 10; } // return true if c is an octal digit inline int isodigit(char c) { return isdigit(c) && c < '8'; } #include "3_11e1.c" /* EXPAND defines TWOSCOMPLEMENT */ int atoi(const char *s) { char sign = 0; int i = 0; switch (*s) // remember the sign { case '+': case '-': sign = *s++; break; } // for positive numbers on a 2's complement machine, // the minimum # is -INT_MAX, not INT_MIN const int int_min = (TWOSCOMPLEMENT && (sign != '-')) ? (-INT_MAX) : INT_MIN; switch (*s) { case '0': // hex or octal switch (*++s) { case 'x': // hex number case 'X': for (s++; isxdigit(*s); s++) { if (-i > INT_MAX / 16) break; i *= 16; int n = hexvalue(*s); if (i < int_min + n) break; i -= n; } if (isxdigit(*s)) error("integer overflow"); break; default: // octal number for ( ; isodigit(*s); s++) { if (-i > INT_MAX / 8) break; i *= 8; int n = *s - '0'; if (i < int_min + n) break; i -= n; } if (isodigit(*s)) error("integer overflow"); break; } break; case '\\': // char constant if (sign == 0) { switch (*++s) { case 'x': // \xXXX -> hex for (int n = 3; isxdigit(*++s) && n-- > 0; ) i = i * 16 - hexvalue(*s); break; // single character constants case 'b': i = -'\b'; s++; break; case 'f': i = -'\f'; s++; break; case 'n': i = -'\n'; s++; break; case 'r': i = -'\r'; s++; break; case 't': i = -'\t'; s++; break; case 'v': i = -'\v'; s++; break; case '\\': i = -'\\'; s++; break; case '\'': i = -'\''; s++; break; case '"': i = -'\"'; s++; break; case '0': i = '\0'; s++; break; default: // \XXX -> octal for (n = 3; isodigit(*s) && n-- > 0; s++) i = i * 8 - (*s - '0'); break; } } else error("char constant with sign"); break; default: // decimal number for ( ; isdigit(*s); s++) { if (-i > INT_MAX / 10) break; i *= 10; int n = *s - '0'; if (i < int_min + n) break; i -= n; } if (isdigit(*s)) error("integer overflow"); break; } if (*s) error("malformed integer"); return sign == '-' ? i : -i; } !EOF! ls -l 3_11c.c echo x - 3_11c.cmp sed 's/^X//' > 3_11c.cmp << '!EOF!' *xp='', i=0, 0x0, 00 *xp='0', i=0, 0x0, 00 *xp='10', i=10, 0xa, 012 *xp='100', i=100, 0x64, 0144 *xp='2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='2147483648', error: integer overflow *xp='123abc', error: malformed integer *xp='+0', i=0, 0x0, 00 *xp='+10', i=10, 0xa, 012 *xp='+100', i=100, 0x64, 0144 *xp='+2147483647', i=2147483647, 0x7fffffff, 017777777777 *xp='+2147483648', error: integer overflow *xp='+123abc', error: malformed integer *xp='-0', i=0, 0x0, 00 *xp='-10', i=-10, 0xfffffff6, 037777777766 *xp='-100', i=-100, 0xffffff9c, 037777777634 *xp='-2147483647', i=-2147483647, 0x80000001, 020000000001 *xp='-2147483648', i=-(, 0x80000000, 020000000000 *xp='-123abc', error: malformed integer *xp='0x0', i=0, 0x0, 00 *xp='0x10', i=16, 0x10, 020 *xp='0x100', i=256, 0x100, 0400 *xp='0x2147483647', error: integer overflow *xp='0x2147483648', error: integer overflow *xp='0x123abc', i=1194684, 0x123abc, 04435274 *xp='+0x0', i=0, 0x0, 00 *xp='+0x10', i=16, 0x10, 020 *xp='+0x100', i=256, 0x100, 0400 *xp='+0x2147483647', error: integer overflow *xp='+0x2147483648', error: integer overflow *xp='+0x123abc', i=1194684, 0x123abc, 04435274 *xp='-0x0', i=0, 0x0, 00 *xp='-0x10', i=-16, 0xfffffff0, 037777777760 *xp='-0x100', i=-256, 0xffffff00, 037777777400 *xp='-0x2147483647', error: integer overflow *xp='-0x2147483648', error: integer overflow *xp='-0x123abc', i=-1194684, 0xffedc544, 037773342504 *xp='0xFfFfFf', i=16777215, 0xffffff, 077777777 *xp='0xFfFfFfFf', error: integer overflow *xp='0x7FfFffff', i=2147483647, 0x7fffffff, 017777777777 *xp='0x8000000', i=134217728, 0x8000000, 01000000000 *xp='0x7g', error: malformed integer *xp='+0xFfFfFf', i=16777215, 0xffffff, 077777777 *xp='+0xFfFfFfFf', error: integer overflow *xp='+0x7fffffff', i=2147483647, 0x7fffffff, 017777777777 *xp='+0x8000000', i=134217728, 0x8000000, 01000000000 *xp='+0x7g', error: malformed integer *xp='-0xFfFfFf', i=-16777215, 0xff000001, 037700000001 *xp='-0xFfFfFfFf', error: integer overflow *xp='-0x7fffffff', i=-2147483647, 0x80000001, 020000000001 *xp='-0x8000000', i=-134217728, 0xf8000000, 037000000000 *xp='-0x7g', error: malformed integer *xp='00', i=0, 0x0, 00 *xp='010', i=8, 0x8, 010 *xp='0100', i=64, 0x40, 0100 *xp='017777777777', i=2147483647, 0x7fffffff, 017777777777 *xp='037777777777', error: integer overflow *xp='020000000000', error: integer overflow *xp='+00', i=0, 0x0, 00 *xp='+010', i=8, 0x8, 010 *xp='+0100', i=64, 0x40, 0100 *xp='+017777777777', i=2147483647, 0x7fffffff, 017777777777 *xp='+037777777777', error: integer overflow *xp='+020000000000', error: integer overflow *xp='-00', i=0, 0x0, 00 *xp='-010', i=-8, 0xfffffff8, 037777777770 *xp='-0100', i=-64, 0xffffffc0, 037777777700 *xp='-017777777777', i=-2147483647, 0x80000001, 020000000001 *xp='-037777777777', error: integer overflow *xp='-020000000000', error: integer overflow *xp='017777777776', i=2147483646, 0x7ffffffe, 017777777776 *xp='017777777778', error: malformed integer *xp='019a', error: malformed integer *xp='+017777777776', i=2147483646, 0x7ffffffe, 017777777776 *xp='+017777777778', error: malformed integer *xp='+019a', error: malformed integer *xp='-017777777776', i=-2147483646, 0x80000002, 020000000002 *xp='-017777777778', error: malformed integer *xp='-019a', error: malformed integer *xp='\344', i=228, 0xe4, 0344 *xp='\x344', i=836, 0x344, 01504 *xp='\b', i=8, 0x8, 010 *xp='\f', i=12, 0xc, 014 *xp='\n', i=10, 0xa, 012 *xp='\r', i=13, 0xd, 015 *xp='\t', i=9, 0x9, 011 *xp='\v', i=11, 0xb, 013 *xp='\\', i=92, 0x5c, 0134 *xp='\'', i=39, 0x27, 047 *xp='\"', i=34, 0x22, 042 *xp='\0', i=0, 0x0, 00 !EOF! ls -l 3_11c.cmp echo x - 3_11d.c sed 's/^X//' > 3_11d.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // convert an integer into a string of max length len // version 1 #include #include void itoa(char *s, int len, int val) { // reverse the sign of negative numbers if (val < 0) { if (--len <= 0) error("string not long enough"); *s++ = '-'; val = -val; } // store digits in reverse order char *p = s; do { if (--len <= 0) error("string not long enough"); *p++ = val % 10 + '0'; val /= 10; } while (val != 0); *p-- = '\0'; // swap the digits around while (s < p) swap(*s++, *p--); } !EOF! ls -l 3_11d.c echo x - 3_11d.cmp sed 's/^X//' > 3_11d.cmp << '!EOF!' *xp='-2147483648', buf='-./,),(-*,(' *xp='0', buf='0' *xp='10', buf='10' *xp='100', buf='100' *xp='2147483647', buf='2147483647' *xp='0', buf='0' *xp='-10', buf='-10' *xp='-100', buf='-100' *xp='-2147483647', buf='-2147483647' *xp='0', buf='0' *xp='16', buf='16' *xp='256', buf='256' *xp='1195914823', buf='1195914823' *xp='1195914824', buf='1195914824' *xp='1194684', buf='1194684' *xp='0', buf='0' *xp='-16', buf='-16' *xp='-256', buf='-256' *xp='-1195914823', buf='-1195914823' *xp='-1195914824', buf='-1195914824' *xp='-1194684', buf='-1194684' *xp='-1', buf='-1' *xp='2147483647', buf='2147483647' *xp='134217728', buf='134217728' *xp='1', buf='1' *xp='-2147483647', buf='-2147483647' *xp='-134217728', buf='-134217728' *xp='0', buf='0' *xp='8', buf='8' *xp='64', buf='64' *xp='2147483647', buf='2147483647' *xp='-1', buf='-1' *xp='-2147483648', buf='-./,),(-*,(' *xp='0', buf='0' *xp='-8', buf='-8' *xp='-64', buf='-64' *xp='-2147483647', buf='-2147483647' *xp='1', buf='1' *xp='-2147483648', buf='-./,),(-*,(' *xp='2147483646', buf='2147483646' *xp='-2147483646', buf='-2147483646' !EOF! ls -l 3_11d.cmp echo x - 3_11dtst.c sed 's/^X//' > 3_11dtst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include #ifdef TSTD #include "3_11d.c" #endif #ifdef TSTE #include "3_11e.c" #endif int x[] = { -2147483648, 0, 10, 100, 2147483647, -0, -10, -100, -2147483647, 0x0, 0x10, 0x100, 0x2147483647, 0x2147483648, 0x123abc, -0x0, -0x10, -0x100, -0x2147483647, -0x2147483648, -0x123abc, 0xFfFfFfFf, 0x7fffffff, 0x8000000, -0xFfFfFfFf, -0x7fffffff, -0x8000000, 00, 010, 0100, 017777777777, 037777777777, 020000000000, -00,-010, -0100, -017777777777, -037777777777, -020000000000, 017777777776, -017777777776, 1515 }; jmp_buf jmpenv; void error(const char *x, ...) { printf("error: %s\n", x); longjmp(jmpenv, 1); } main() { char buf[100]; for (int *xp = x; *xp != 1515; xp++) if (setjmp(jmpenv)) ; // error occurred else { itoa(buf, sizeof(buf), *xp); printf("*xp='%d', buf='%s'\n", *xp, buf); } return 0; } !EOF! ls -l 3_11dtst.c echo x - 3_11e.c sed 's/^X//' > 3_11e.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // convert an integer into a string of max length len // version 2 #include #include #include #include "3_11e1.c" /* EXPAND defines TWOSCOMPLEMENT */ #include "3_11e2.c" /* EXPAND defines BITS() */ #include "3_11e3.c" /* EXPAND defines HIBIT() */ void itoa(char *s, int len, int val) { char *p; // reverse the sign of negative numbers if (val < 0) { if (--len <= 0) error("string not long enough"); *s++ = '-'; p = s; if (TWOSCOMPLEMENT && (val == INT_MIN)) { if (--len <= 0) error("string not long enough"); #include "3_11e4.c" /* EXPAND12 divides by 10 */ } else val = -val; } else p = s; // store digits in reverse order do { if (--len <= 0) error("string not long enough"); *p++ = val % 10 + '0'; val /= 10; } while (val != 0); *p-- = '\0'; // swap the digits around while (s < p) swap(*s++, *p--); } !EOF! ls -l 3_11e.c echo x - 3_11e.cmp sed 's/^X//' > 3_11e.cmp << '!EOF!' *xp='-2147483648', buf='-2147483648' *xp='0', buf='0' *xp='10', buf='10' *xp='100', buf='100' *xp='2147483647', buf='2147483647' *xp='0', buf='0' *xp='-10', buf='-10' *xp='-100', buf='-100' *xp='-2147483647', buf='-2147483647' *xp='0', buf='0' *xp='16', buf='16' *xp='256', buf='256' *xp='1195914823', buf='1195914823' *xp='1195914824', buf='1195914824' *xp='1194684', buf='1194684' *xp='0', buf='0' *xp='-16', buf='-16' *xp='-256', buf='-256' *xp='-1195914823', buf='-1195914823' *xp='-1195914824', buf='-1195914824' *xp='-1194684', buf='-1194684' *xp='-1', buf='-1' *xp='2147483647', buf='2147483647' *xp='134217728', buf='134217728' *xp='1', buf='1' *xp='-2147483647', buf='-2147483647' *xp='-134217728', buf='-134217728' *xp='0', buf='0' *xp='8', buf='8' *xp='64', buf='64' *xp='2147483647', buf='2147483647' *xp='-1', buf='-1' *xp='-2147483648', buf='-2147483648' *xp='0', buf='0' *xp='-8', buf='-8' *xp='-64', buf='-64' *xp='-2147483647', buf='-2147483647' *xp='1', buf='1' *xp='-2147483648', buf='-2147483648' *xp='2147483646', buf='2147483646' *xp='-2147483646', buf='-2147483646' !EOF! ls -l 3_11e.cmp echo x - 3_11e1.c sed 's/^X//' > 3_11e1.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ const int TWOSCOMPLEMENT = (-INT_MAX != INT_MIN); !EOF! ls -l 3_11e1.c echo x - 3_11e2.c sed 's/^X//' > 3_11e2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #undef BITS /* DELETE */ #define BITS(type) (CHAR_BIT * (int)sizeof(type)) !EOF! ls -l 3_11e2.c echo x - 3_11e3.c sed 's/^X//' > 3_11e3.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #define HIBIT(type) ((type)(((type) 1) << \ (BITS(type) - 1))) !EOF! ls -l 3_11e3.c echo x - 3_11e4.c sed 's/^X//' > 3_11e4.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // divide by 10 the hard way val = (val >> 1) & ~HIBIT(int); val /= 5; *p++ = (val % 5 * 2 + '0'); !EOF! ls -l 3_11e4.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.a all: 3_11atst 3_11abtst 3_11btst 3_11ctst 3_11dtst 3_11etst 3_11atst: 3_11atst.c 3_11a.c $(CC) -DTSTA 3_11atst.c -o 3_11atst 3_11abtst: 3_11atst.c 3_11a_b.c $(CC) -DTSTAB 3_11atst.c -o 3_11abtst 3_11btst: 3_11atst.c 3_11b.c $(CC) -DTSTB 3_11atst.c -o 3_11btst 3_11ctst: 3_11atst.c 3_11c.c $(CC) -DTSTC 3_11atst.c -o 3_11ctst 3_11dtst: 3_11dtst.c 3_11d.c $(CC) -DTSTD 3_11dtst.c -o 3_11dtst $(ERROR) 3_11etst: 3_11dtst.c 3_11e.c $(CC) -DTSTE 3_11dtst.c -o 3_11etst $(ERROR) CMP= 3_11a.cmp 3_11ab.cmp 3_11b.cmp 3_11c.cmp 3_11d.cmp 3_11e.cmp OUT= 3_11a.out 3_11ab.out 3_11b.out 3_11c.out 3_11d.out 3_11e.out 3_11a.out: 3_11atst; 3_11atst > 3_11a.out 3_11ab.out: 3_11abtst; 3_11abtst > 3_11ab.out 3_11b.out: 3_11btst; 3_11btst > 3_11b.out 3_11c.out: 3_11ctst; 3_11ctst > 3_11c.out 3_11d.out: 3_11dtst; 3_11dtst > 3_11d.out 3_11e.out: 3_11etst; 3_11etst > 3_11e.out test: all $(CMP) $(OUT) cmp 3_11a.out 3_11a.cmp cmp 3_11ab.out 3_11ab.cmp cmp 3_11b.out 3_11b.cmp cmp 3_11c.out 3_11c.cmp cmp 3_11d.out 3_11d.cmp cmp 3_11e.out 3_11e.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0