home *** CD-ROM | disk | FTP | other *** search
-
- A few of Dr. Doom's list of C-gotchas, condensed considerably from the
- originals:
-
- 1. EOF is an int, not a char; "getchar()", despite its
- character-sounding name, RETURNS AN INT! In fact, all functions in the
- C language return either an int or a double; it is not possible to
- write a function which returns a char. Besides, the compiler generates
- code which takes the same amount of space for (scalar) chars and ints.
- So: there is NEVER any reason to say, for example: "char c;". You
- should ALWAYS declare c to be an int, especially when it receives the
- return value from getchar, which is then compared to EOF.
-
- Examples:
-
- /* this is WRONG (WRONG!, WRONG!, WRONG! ...*/
- char c;
- while( (c=getchar()) != EOF){ ... }
-
- /* this is RIGHT */
- int c;
- while( (c=getchar()) != EOF){ ... }
-
- ----------------------------------------------------
-
- 2. Beware the distinction between "=" and "==". The single-
- equal-sign (we'll call it SE) causes the things on the two sides of it
- to BECOME equal; the resultant value is then whatever value was
- originally on the right-hand-side of the SE operator. The double-equal
- sign (DE) first tests the two sides to see if they are equal; the
- resulting value of the DE operator is zero if they are not equal, and
- one if they are equal. No other results are possible.
-
- Examples:
-
- /* this is (probably) WRONG */
- int i,j;
- if(i=j){ ... } /*will execute if j is nonzero */
-
- /* this is (probably) RIGHT */
- int i,j;
- if(i==j){ ... } /*will execute if i is equal to j */
-
- ----------------------------------------------------
-
- 3. Even experienced programmers sometimes forget the "&"s which
- are required on the arguments to scanf. All arguments after the
- first must have "&" in front of it.
-
- Examples:
- /* WRONG ... guaranteed to dump core */
- int i;
- scanf("%d",i);
-
- /* RIGHT */
- int i;
- scanf("%d",&i);
-
- /* for advanced students only: */
- int j, *i = &j;
- scanf("%d",i);
- printf("%d %d\n",i,j); /* what does it print? */
-
- ----------------------------------------------------
-
- 4. Beware the extraneous semicolon.
-
- /* (probably) WRONG */
- for(i=0; i<10; i++);
- printf("%d\n",i);
-
- /* (probably) RIGHT */
- for(i=0; i<10; i++)
- printf("%d\n",i);
-
- ----------------------------------------------------
-
- 5. Again, even experienced programmers sometimes (Hi, Chris!)
- get bit by the "dangling else" bug:
-
- if(i==5)
- k=7;
- if (line==27) d = 39;
- else
- d = 41;
-
- and variations too numerous to show here. The "else" goes with the
- nearest preceding "if" (well, actually, the nearest preceding
- "if" that doesn't already have an "else" associated with it).
- The compiler does not know indentation.
-
- ----------------------------------------------------
-
- 6. Scanf() from the terminal can produce some perplexing results
- until you learn about "the terminating newline", and what scanf
- considers to be whitespace for %d, %s, and %c formats. Primarily
- try to remember that for a "%d" format, scanf IGNORES leading
- whitespace, and PUTS BACK the whitespace which caused the termination
- of a "%d" scan. Moral of the story: "do not mix %c and %s formats
- with %d or %f formats, nor mix scanf with getchar, unless you are
- very careful AND know what you're doing."
-