home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13664 < prev    next >
Encoding:
Text File  |  1992-09-14  |  2.4 KB  |  60 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news
  3. From: scs@adam.mit.edu (Steve Summit)
  4. Subject: Re: sscanf and portability
  5. Message-ID: <1992Sep15.005935.19233@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: adam.mit.edu
  8. Organization: none, at the moment
  9. References: <RUJO.92Sep14160231@ulrik.uio.no>
  10. Date: Tue, 15 Sep 1992 00:59:35 GMT
  11. Lines: 47
  12.  
  13. In article <RUJO.92Sep14160231@ulrik.uio.no>, rujo@ulrik.uio.no (Rune J|rgensen) writes:
  14. > If you use types that explisit defines a byte size, e.g.:
  15. > typedef short INT16;
  16. > typedef long  INT32;
  17. > INT16 i16;
  18. > INT32 i32;
  19. > How do you preserve portability with sscanf? E.g:
  20.  
  21. Mit Schwierigkeit, as they say in Deutschland.
  22.  
  23. Defining types with precise numbers of bits is a bit of a
  24. perversion on the intent of C's type system, so it is not too
  25. surprising that occasional difficulties like this arise.  When
  26. precisely-sized types are truly necessary, typedefs like the
  27. above are the right way to implement them, but their necessity
  28. should be rare.  (I don't believe I have ever used such a type.)
  29.  
  30. The traditional need for precisely-sized types arises of course
  31. when people fall into the trap of using "binary" data files or
  32. otherwise attempting to conform to externally-imposed storage
  33. layouts.  Such situations should themselves be much rarer than
  34. they are, further minimizing the occurrence of the problem which
  35. Rune mentions.  Nevertheless, if a program does use precisely-sized
  36. variables to facilitate "binary" I/O, it should have fewer places
  37. where it needs to perform text I/O (scanf/printf) using those
  38. variables.
  39.  
  40. Obviously, some of the numbers which are read from or written to
  41. a data file must be respectively presented to or requested from
  42. the user, but scanf is a notoriously poor vehicle for interactive
  43. input and should itself be avoided, and printf uses values rather
  44. than pointers such that conservative format-specifiers and
  45. explicit casts solve the problem rather easily:
  46.  
  47.     printf("%d %ld", (int)i16, (long)i32);
  48.  
  49. If I were faced with this problem, I'd ask myself carefully
  50. whether I really needed to be using precisely-sized types at all,
  51. and whether I really needed to use scanf.  If both were in fact
  52. absolutely necessary, the solution (using temporary variables and
  53. extra assignments) which Lars Wirzenius has already outlined
  54. would be about the best that could be done.
  55.  
  56.                     Steve Summit
  57.                     scs@adam.mit.edu
  58.