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 />

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

know, but that is another story...<br />

Some problems with C<br />

The language was designed with writing operating systems in mind, and so it was not purpose-built for numerical<br />

work.<br />

There is no support for complex numbers (actually, as <strong>of</strong> the C-99 standard, there is!).<br />

Handling multi-dimensional arrays, understanding pointers, and string manipulation are difficult when you first<br />

encounter them.<br />

It is easy to write completely opaque code in C.<br />

You have to explicitly declare every variable that you use. This is actually not a problem at all! It is a feature.<br />

It is possible to bypass many <strong>of</strong> the protective features <strong>of</strong> the language, and get yourself into trouble<br />

(alternatively, this can be seen as an advantage, since the language imposes few limitations on what you can do).<br />

The ANSI C standard leaves a lot <strong>of</strong> details undefined. This makes C non-portable between different operating<br />

systems, compilers, and computer hardware. Java is better in this respect.<br />

It is not easy to write large bug-free programs in C. Java is <strong>of</strong>ten a better choice.<br />

A brief history <strong>of</strong> C<br />

C evolved from a language called B, written by Ken Thompson at Bell Labs in 1970. Ken used B to write one <strong>of</strong> the<br />

first implementations <strong>of</strong> UNIX. B in turn was a decendant <strong>of</strong> the language BCPL (developed at Cambridge (UK) in<br />

1967), with most <strong>of</strong> its instructions removed.<br />

In fact, so many instructions were removed in going from BCPL to B, that Dennis Ritchie <strong>of</strong> Bell Labs put some back<br />

in (in 1972), and called the language C.<br />

The famous book The C Programming Language was written by Kernighan and Ritchie in 1978, and has remained the<br />

definitive reference book on C.<br />

The original C was still too limiting, and not standardized, and so in 1983 an ANSI committee was established to<br />

formalise the language definition.<br />

By 1993 the ANSI standard became well accepted and almost universally supported by compilers. The GNU C<br />

compiler, gcc, has become, in many ways, the standard implementation <strong>of</strong> C.<br />

What about C++ and C#?<br />

C++ is a superset <strong>of</strong> C, incorporating many object-oriented <strong>programming</strong> ideas. However, it is not particularly widely<br />

used. Java is a better choice in most cases.<br />

C# is a Java-like language developed by Micros<strong>of</strong>t. Enough said.<br />

Interesting books<br />

Numerical Analysis by Burden and Faires.<br />

A New Kind <strong>of</strong> Science by Wolfram.<br />

Numerical Recipies in C<br />

Other on-line resources<br />

For coverage <strong>of</strong> typical problems encountered with C, have a look at the Comp.lang.c FAQ.<br />

For a rather good tutorial introduction to C, check out this document by Gordon Dodrill.<br />

The simplest possible C program<br />

Here it is.<br />

Hello world

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

Saved successfully!

Ooh no, something went wrong!