Common mistakes with input and output
Comments and questions to John Rowe.
|
|
| Wrong
| Right
|
|
|
| Since scanf() is an intelligent function: it
skips over white space such as spaces and new-lines unless we
actively stop it. Such as the next mistake...
|
|
For example, putting a comma after "%d" when reading two integers:
|
| Wrong
| Right
|
|
|
| Note, this isn't always wrong
(for example we may be reading from a file with a fixed
data format), but we should avoid it if possible.
|
|
|
| Wrong
| Right
|
|
|
| Again, this isn't always wrong but in general it's
much better to tell the user what they are supposed to be entering.
|
|
For variety we show a situation where we choose to warn of
the failure to open a file rather than exit the program:
|
| Wrong
| Right
|
|
|
|
You only need to worry about these if you have not
followed our advice...
|
Forgetting the ampersand
|
| Wrong
| Right
|
|
|
Not using %lg to read a double
|
| Wrong
| Right
|
|
|
Log in