11.07.2014 Views

C programming notes - School of Physics

C programming notes - School of Physics

C programming notes - School of Physics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C <strong>programming</strong> <strong>notes</strong><br />

file:///F:/my_docs/web_phys2020/C<strong>programming</strong><strong>notes</strong>.html<br />

8 <strong>of</strong> 40 19/03/2007 10:06 AM<br />

command | ./a.out<br />

standard input comes from the standard output <strong>of</strong> "command"<br />

Standard output<br />

Standard output (or STDOUT) is a stream <strong>of</strong> bytes produced by a program. Standard output goes to one <strong>of</strong> three<br />

locations depending on how you run your program. E.g.,<br />

./a.out<br />

./a.out > file<br />

./a.out | command<br />

standard output appears on the computer screen as text<br />

standard output is sent to the file called "file"<br />

standard output is sent to the standard input <strong>of</strong> "command"<br />

Standard error<br />

Standard error (or STDERR) is a stream <strong>of</strong> bytes produced by a program. Usually it contains error messages<br />

(e.g., 'divide by zero') that are not normally considered as part <strong>of</strong> the program output. Standard error can be sent<br />

to one <strong>of</strong> three locations. E.g.,<br />

./a.out<br />

./a.out 2> file<br />

./a.out 2>&1 | command<br />

standard error appears on the computer<br />

screen as text, intermingled with the standard output<br />

standard error is sent to the file called "file"<br />

standard error (and standard output) is sent to<br />

the standard input <strong>of</strong> "command"<br />

Command-line arguments<br />

Anything you type after the program name, and before any special shell characters such as '>' and '|', is made<br />

available to your program as a command-line argument. These can be a useful source <strong>of</strong> additional input to a<br />

program. E.g.,<br />

./a.out 23.45 albatross 7<br />

In the above example, "23.45", "albatross", and "7" are three command-line arguments.We will see later how you<br />

can read them from within a C program.<br />

Exit status<br />

Another form <strong>of</strong> output that is available to all programs is an integer exit status. By convention, this is used to<br />

indicate whether the program ran successfully (in which case the exit status is zero) or if an error occurred (in<br />

which case the exit status is nonzero and its value can be decoded to determine what the error was). The exit<br />

status <strong>of</strong> a program is stored in the shell variable called "$?" (don't worry if you don't understand this at this<br />

stage).<br />

Putting it all together<br />

Here are some more complex examples <strong>of</strong> program input/output:<br />

./a.out 2.1 < data.txt > results.txt<br />

"2.1" is an argument, standard input<br />

comes from "data.txt", standard output<br />

goes to "results.txt"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!