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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

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

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

Note that there are some redundant bit patterns in the representation <strong>of</strong> floating-point numbers: e.g., 0.0 with any<br />

exponent is still zero. One <strong>of</strong> these bit patterns is used as a flag to indicate that a calculation produced an illegal result.<br />

This bit pattern is called "NaN" which stands for "not a number". E.g, if you take the square root <strong>of</strong> a negative number,<br />

you will receive the result NaN. Any operation on a NaN will continue to produce a NaN (e.g., if you subtract two<br />

NaNs you will still have a NaN), this is useful to propagate such error conditions in your programs so that you are<br />

aware when they affect the final result <strong>of</strong> a calculation.<br />

GNU/Linux Fundamentals<br />

The operating system<br />

The operating system <strong>of</strong> a computer is, somewhat loosely speaking, the program that is always operating in the<br />

background on the computer. It handles the interaction with the hardware, and running other programs for the user(s) <strong>of</strong><br />

the computer. The operating system that we will be using is called GNU/Linux. Other examples <strong>of</strong> operating systems<br />

are Windows XP, OpenBSD, and FreeBSD.<br />

The shell<br />

The shell is an interactive program that allows the user to interface with the operating system. This is the program that<br />

provides the command-line prompt (it also helps you run programs, allows you to use the arrow keys on your keyboard<br />

to edit the command-line, and a bunch <strong>of</strong> other useful things). The shell that we will be using is called bash, it is the<br />

standard shell that is used with GNU/Linux. Other examples <strong>of</strong> shells are ksh, tcsh, and zsh.<br />

Files<br />

A file consists <strong>of</strong> two components:<br />

File data<br />

The data in a file is simply a list <strong>of</strong> bytes. The list may be empty (i.e., the file may have no data). The list <strong>of</strong> bytes<br />

might be used for a variety <strong>of</strong> different purposes, for example, it may be a C program, an e-mail message, a<br />

photograph, a song, a movie. In principle there is no way to distinguish the purpose <strong>of</strong> a file's data by looking at<br />

it. In practice, you can usually make a pretty good guess. To list the data in a file, you can, e.g., use "cat myfile".<br />

File metadata<br />

The metadata associated with a file consists <strong>of</strong> miscellaneous additional information that the operating system<br />

keeps with the file. For example: the name <strong>of</strong> the file, the number <strong>of</strong> bytes in the file, the time at which the file<br />

was created, which user owns the file, and the file permissions (i.e., the list <strong>of</strong> users who read and/or write the<br />

file, and whether the file is executable (i.e., is a program that can be run)). The term "metadata" means "data<br />

about data". To list the metadata associated with a file, you can, e.g., use "stat myfile".<br />

File names and file extensions<br />

When you create a file, you give it a name. The name is a list <strong>of</strong> bytes. In principle the name could be bizarre, such as<br />

" _1 `", but in practice you should choose simple, lowercase, names without spaces, e.g., "thesis.txt", "photo.jpg",<br />

"assignment0001.c". Note the convention <strong>of</strong> suffixing a file name with an extension that gives a clue as what the file is<br />

being used for. So, C programs invariably end with the ".c" extension.<br />

The GNU/Linux filesystem<br />

Directories (folders)<br />

To assist with organising groups <strong>of</strong> files, GNU/Linux has the concept <strong>of</strong> a directory (or folder). A directory is actually<br />

just a file (this adheres to the UNIX philosophy <strong>of</strong> making things simple and logical). A directory contains a list <strong>of</strong> the<br />

file names that are "in" the directory. Directories can contain directories. This leads to a hierarchical filesystem, where,<br />

in order to fully specify the location <strong>of</strong> a file, you have to specify the list <strong>of</strong> directories to follow to reach the file. By<br />

convention a slash character '/' is used to separate the directory names when giving the fully qualified filename. "/" itself<br />

is known as the root directory - there are no directories above the root directory in the filesystem. "/thesis.txt" would

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

Saved successfully!

Ooh no, something went wrong!