CSA2060: Programming in C

FAQ

What's EOF?

EOF is a symbolic constant which represents the End Of File marker. It is defined in stdio.h and is platform dependent. EOF is automatically written to the end of a file, but if your program is reading data from the keyboard, then you have to type the EOF character, which must be the first character of a new line. On UNIX, EOF is CTRL-D (press the Control key and the D key together). On Windows/DOS, you must use CTRL-Z.

How can I generate an executable file called something other than a.out?

There are two ways to do this. Either compile the source code using gcc prog.c -o myFile, where myFile is the name of the executable file, or else mv a.out myFile, which renames the default executable file a.out to a filename of your choice. You can then run the program by typing myFile at the command line prompt. See Lecture 1. This assumes you're using the gcc C compiler on UNIX.

I ran the character input and output program, which reads and writes a character at a time, but when I run it, it prints a whole line at a time instead. Why?

It outputs a whole line at a time, rather than a character at a time, because the functions that we are using (e.g., putchar, printf) use buffered output. These functions write characters to a buffer (which is associated with standard output), and the buffer is flushed to the output device (in our case, the monitor) when either a new line or the end of program are detected.

The simplest way to see this is to run the following program:

#include 

main() {
 putchar('a');
 putchar('\n');
 while (1) ;
}

This prints an a and a new line and then loops forever, until you interrupt the program (CTRL-C on UNIX). If you run it it will print an a and then loop. If you comment out or delete the putchar('\n'), however, and re-run it (remembering to re-compile it first!), then the a will not be printed, although the program will still loop forever. Typing CTRL-C will not cause a to be printed, because the program is terminated abnormally. Something similar is happening with the input functions too. The input is buffered and is processed only once a new line or end of file marker is detected.