hdsalbki said:
Thank you for our comment Walter. Actually in my book the \n is given
at the end of the prompt...I changed it to the front because otherwise
the extra prompt was getting printed on the same line as the character
echoed.
Putting a '\n' at the beginning of a prompt is something I've seen
mostly in Windows code, and I don't understand why.
If you want to print a line, put the '\n' at the end of the line. If
you want to print a prompt, with the input to be typed at the end of
the line, don't use '\n' at all. But printing a partial line like
that does complicate things a bit, specifically:
I don't know what fflush(stdout) (just in the second chapter)
but I will include it.
Yes, you should have the fflush(stdout), but don't be surprised if it
doesn't make any visible difference. Here's what's happening.
You're writing to standard output, which is a text stream. Any text
stream can be "unbuffered", "line buffered", or "fully buffered".
This determines when output written to the stream actually appears.
For an unbuffered stream, output appears immediately. For a line
buffered stream, it appears only when a line has been completed. For
a fully buffered stream, it appears when a buffer has been filled; the
size of the buffer can vary. (The latter is typical for writing to
disk files.)
If standard output is going to an interactive device, such as a
terminal or emulator, it's required to be either unbuffered or line
buffered. If it's line buffered, you need the call to fflush(stdout)
to force a partial line to appear. *However*, on most modern systems,
standard output for an interactive program is likely to be unbuffered,
which means that fflush(stdout) will have no effect. Adding the
fflush(stdout) is a precaution that may not be necessary for your
system, but it is necessary if you want your program to be maximally
portable. (Line buffering goes back to the days of hardcopy line
printers.)
wrt to scanf how can I get an end-of-file or I/
O error?
You can get end-of-file if you reach the end of the input file. If
you were reading from a disk file, that would simply be when you've
read everything from the file. For interactive keyboard input,
there's generally a system-specific way to trigger an end-of-file
condition; it's usually control-D for Unix-like systems, control-Z for
DOS/Windows-like systems. (Your program won't actually see the
control-D or control-Z character.)
And main() returns nothing...that's why I didn't return
anything... is that wrong? My book has this everywhere (in someplaces
it is main(void) ).
What book are you using?
If you write
main() { ... }
then the main function is *implicitly* declared to return type int. In other
words, this:
int main() { ...}
and this:
main() { ... }
are equivalent.
The best way to define main is:
int main(void) { ... }
or, if you want to accept command-line arguments:
int main(int argc, char **argv) { ... }
In either case, you *should* return an int value. Returning 0 is
usually the best. Add "return 0;" just before the closing brace.
Leaving off the "int", using "()" rather than "(void)", and leaving
out the return statement are all things that you can *probably* get
away with, but your compiler *might* complain about one or more of
them, depending on various things that I won't go into. (Declaring
main as "void main()" is another matter; don't do that, and avoid any
books that tell you you should.) Doing it right is a good habit, and
it can't hurt.
and I kill the program with ctrl-c because I
wanted to echo all that the user types. I get it now. So I think to
fix this I can have another scanf() call before my main one and just
read the '\n' to a dummy variable? Is this okay?
Once again thanks for your input.
As discussed elsewhere, scanf() isn't the best tool for
single-character input. Actually, it's not the best tool for much of
anything. It can be deceptively simple for simple usage, but once you
try to make your code more robust, it's just more trouble than it's
worth (IMHO).