home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / std_unix / volume.31 / text0047.txt < prev    next >
Encoding:
Text File  |  1993-07-15  |  3.2 KB  |  87 lines

  1. Submitted-by: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  2.  
  3. In article <1rro2qINNk3f@ftp.UU.NET>, davidm@cimshop.UUCP (David S. Masterson) writes:
  4. > Submitted-by: davidm@cimshop.UUCP (David S. Masterson)
  5. > Shouldn't 'printf "%c" 3' output a NULL byte?  I would expect that the '3'
  6. > would be stored in as a 4 byte integer of which the first 3 bytes are '0'.
  7. > The '%c' should take the byte at the address of the argument to printf (not
  8. > what that argument points to) and output it as an ASCII character (in this
  9. > case NULL).  Without testing it, I'm not sure -- is this not what happens?
  10.  
  11. Recall that
  12. (1) The existing documentation for Chris Torek's printf(1)  -- and the
  13.     undocumented intent of mine, which unfortunately uses proprietary
  14.     Quintus code for the underlying printf() engine -- has it that
  15.  
  16.     Printf duplicates (as far as possible) the standard C
  17.     library routine of the same name
  18.  
  19.     In C, printf("%c", 3) writes a byte with value \003 (unless of course
  20.     '\n' == '\003', in which case strange things might happen).  It does
  21.     not write a NUL byte.
  22.  
  23. (2) There are at least _three_ languages in *IX which have printf():
  24.     (A) C
  25.     (B) AWK -- which is part of .2
  26.     (C) SH -- the printf(1) command
  27.     So we would expect that
  28.     printf "%c" 3
  29.     and
  30.     awk -e 'END { printf("%c", 3) }' </dev/null
  31.     would produce the same results.
  32.  
  33. (3) Who said that the printf(1) command uses 4 byte integers?
  34.  
  35. (4) "The '%c' should take the byte at the address of the argument to
  36.     printf".  WHAT address?  In the C equivalent, namely
  37.     printf("%c", 3)
  38.     printf() receives the numeric value 3, not the address of anything.
  39.     Certainly a command line notionally contains no addresses.
  40.  
  41. (5) For what it's worth, the relevant part of the code in my printf(1)
  42.     looks like this:
  43.  
  44. long get_integer_arg(operands, size)
  45.     char ***operands;
  46.     int size;
  47.     {
  48.         char *arg = *++*operands;
  49.     long result;
  50.  
  51.         if (!arg) insufficient_arguments();
  52.         result = expr(arg);        /* copied from PD M4's eval() */
  53.     switch (size) {
  54.         case BYTE_SIZE: return result & 255;
  55.         case HALF_SIZE: return result & 65535;
  56.         case LONG_SIZE: return result;
  57.     }
  58.     }
  59.  
  60.                     case 'c':
  61.             c = get_integer_arg(&operands, operand_size);
  62.                         /* drop through */
  63.                     default:
  64. literal:                if (precision < 0) precision = 1;
  65.                         if ((field_width -= precision) <= 0) {
  66.                             PUTCHR(c, precision);
  67.                         } else
  68.                         if (justification == GAMMA_PADDING) {
  69.                             PUTCHR(c, precision);
  70.                             PUTCHR(padding_character, field_width);
  71.                         } else {
  72.                             PUTCHR(padding_character, field_width);
  73.                             PUTCHR(c, precision);
  74.                         }
  75.                         break;
  76.  
  77.     PUTCHAR(char, count) writes out count copies of the character (stored
  78.     in) char.  So, for my implementation, it would make exactly as much
  79.     sense for 'printf %c 3' to write a NUL character as it would for
  80.     'printf "x"' to write a NUL character; the same code handles both.
  81.     The only address here is operands, which is a pointer into argv[].
  82.  
  83.  
  84.  
  85. Volume-Number: Volume 31, Number 50
  86.  
  87.