home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / help.jn / C-mistakes.txt < prev    next >
Encoding:
Text File  |  1989-08-19  |  3.1 KB  |  102 lines

  1.  
  2. A few of Dr. Doom's list of C-gotchas, condensed considerably from the
  3. originals:
  4.  
  5. 1.  EOF is an int, not a char; "getchar()", despite its
  6. character-sounding name, RETURNS AN INT!  In fact, all functions in the
  7. C language return either an int or a double; it is not possible to
  8. write a function which returns a char.  Besides, the compiler generates
  9. code which takes the same amount of space for (scalar) chars and ints.
  10. So: there is NEVER any reason to say, for example: "char c;".  You
  11. should ALWAYS declare c to be an int, especially when it receives the
  12. return value from getchar, which is then compared to EOF.
  13.  
  14. Examples:
  15.  
  16.     /* this is WRONG (WRONG!, WRONG!, WRONG! ...*/
  17.     char c;
  18.     while(  (c=getchar()) != EOF){ ... }
  19.  
  20.     /* this is RIGHT */
  21.     int c;
  22.     while(  (c=getchar()) != EOF){ ... }
  23.  
  24. ----------------------------------------------------
  25.  
  26. 2.  Beware the distinction between "=" and "==".  The single-
  27. equal-sign (we'll call it SE) causes the things on the two sides of it
  28. to BECOME equal; the resultant value is then whatever value was
  29. originally on the right-hand-side of the SE operator.  The double-equal
  30. sign (DE) first tests the two sides to see if they are equal; the
  31. resulting value of the DE operator is zero if they are not equal, and
  32. one if they are equal.  No other results are possible.  
  33.  
  34. Examples:
  35.  
  36.     /* this is (probably) WRONG */
  37.     int i,j;
  38.     if(i=j){ ... }  /*will execute if j is nonzero */
  39.  
  40.     /* this is (probably) RIGHT */
  41.     int i,j;
  42.     if(i==j){ ... } /*will execute if i is equal to j */
  43.  
  44. ----------------------------------------------------
  45.  
  46. 3.  Even experienced programmers sometimes forget the "&"s which
  47. are required on the arguments to scanf.  All arguments after the
  48. first must have "&" in front of it.
  49.  
  50. Examples:
  51.     /* WRONG ... guaranteed to dump core */
  52.     int i;
  53.     scanf("%d",i);
  54.  
  55.     /* RIGHT */
  56.     int i;
  57.     scanf("%d",&i);
  58.  
  59.     /* for advanced students only: */
  60.     int j, *i = &j;
  61.     scanf("%d",i);
  62.     printf("%d %d\n",i,j); /* what does it print? */
  63.  
  64. ----------------------------------------------------
  65.  
  66. 4.  Beware the extraneous semicolon.
  67.  
  68.     /* (probably) WRONG */
  69.     for(i=0; i<10; i++);
  70.         printf("%d\n",i);
  71.  
  72.     /* (probably) RIGHT */
  73.     for(i=0; i<10; i++)
  74.         printf("%d\n",i);
  75.  
  76. ----------------------------------------------------
  77.  
  78. 5.  Again, even experienced programmers sometimes (Hi, Chris!)
  79. get bit by the "dangling else" bug:
  80.  
  81.     if(i==5)
  82.         k=7;
  83.         if (line==27) d = 39;
  84.     else
  85.         d = 41;
  86.  
  87. and variations too numerous to show here.  The "else" goes with the
  88. nearest preceding "if" (well, actually, the nearest preceding
  89. "if" that doesn't already have an "else" associated with it).  
  90. The compiler does not know indentation.
  91.  
  92. ----------------------------------------------------
  93.  
  94. 6.  Scanf() from the terminal can produce some perplexing results
  95. until you learn about "the terminating newline", and what scanf
  96. considers to be whitespace for %d, %s, and %c formats.  Primarily
  97. try to remember that for a "%d" format, scanf IGNORES leading
  98. whitespace, and PUTS BACK the whitespace which caused the termination
  99. of a "%d" scan.  Moral of the story: "do not mix %c and %s formats
  100. with %d or %f formats, nor mix scanf with getchar, unless you are
  101. very careful AND know what you're doing."
  102.