C programming notes - School of Physics
C programming notes - School of Physics
C programming notes - School of Physics
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 />
28 <strong>of</strong> 40 19/03/2007 10:06 AM<br />
first element is "table[0][0][0]", the next element is "table[0][0][1]", and the last element is "table[1][2][3]").<br />
When writing programs that use large arrays (say, more than a few megabytes), you should be very careful to ensure<br />
that array references in your program are as close as possible to being consecutive (otherwise you may get severe<br />
swapping problems, i.e., the memory is swapped to hard disk, resulting in more than a factor <strong>of</strong> 1000 slow-down).<br />
Initialisation <strong>of</strong> multidimensional arrays<br />
This is best demonstrated with an example:<br />
int mat[3][4] = {<br />
}<br />
Operations on arrays<br />
{ 0, 1, 2, 23},<br />
{ 3, 4, 5, 24},<br />
{ 6, 7, 8, 25}<br />
If you wish to, e.g., add two arrays together, you have to do it element-by-element yourself using a "for" loop. For<br />
example:<br />
int a0[10], a1[10], a2[10];<br />
for (int i = 0; i < 10; i++) {<br />
a0[i] = a1[i] + a2[i];<br />
}<br />
Even something as simple as setting one array to have the same value as another has to be done with a "for" loop. E.g. :<br />
int a0[10], a1[10];<br />
for (int i = 0; i < 10; i++) {<br />
a0[i] = a1[i];<br />
}<br />
Character arrays<br />
Arrays can be <strong>of</strong> any type. In C, you manipulate character strings as arrays <strong>of</strong> characters, and operations on the strings<br />
(such as concatenation, searching) are done by calling special libary functions (e.g., strcat, strcmp).<br />
Note that when calling a string-manipulation function, the end <strong>of</strong> the string is taken as the position <strong>of</strong> the first NUL<br />
character (which has a numerical value <strong>of</strong> zero) in the string.<br />
Character arrays can be initialised in the following ways:<br />
char str[] = {'a', 'b', 'c'};<br />
char prompt[] = "please enter a number";<br />
In the example, "str" has length 3 bytes, and "prompt" has length 22 bytes, which is one more than the number <strong>of</strong><br />
characters in "please enter a number", the extra byte is used to store a NUL character (zero) as an indication <strong>of</strong> the end<br />
<strong>of</strong> the string. "str" does not having a trailing NUL.<br />
Functions<br />
A function call allows a commonly used piece <strong>of</strong> code to be made available through a single calling statement. For<br />
instance, to calculate the square <strong>of</strong> a number we could use the following (floating point) function:<br />
float sqr(inval) /* square a float, return a float */<br />
float inval;<br />
{<br />
float square;<br />
square = inval * inval;<br />
return(square);