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

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

}<br />

r = r - 1.2F;<br />

printf("%22.16e\n", r);<br />

return 0;<br />

The program gives at output <strong>of</strong> "-4.4703483581542969e-08", not zero as you might expect.<br />

EXERCISE: Run this program and verify for yourself that the answer is indeed not zero! Re-write the program so that<br />

the numerical values in the code (i.e. 0.2, 1.2 etc) are double rather than float, and verify that the result <strong>of</strong> execution is<br />

to give the answer 0.000000000000000e+00.<br />

The lesson to be learnt here is when writing constants, always think carefully about what type you want them to be, and<br />

use the suffixes "U", "L", and "F" to be explicit about it. It is not a good idea to rely on the compiler to do what you<br />

expect. Don't be surprised if different machines/compilers give different answers if you program sloppily.<br />

Conversion between integers and floating point numbers<br />

In C, as in all computer languages, there are rules that the compiler uses when a program mixes integers and floating<br />

point numbers in the same expression.<br />

Let's look at what happens if you assign a floating point number to an integer variable (convert1.c):<br />

#include <br />

int main(void) {<br />

int i, j;<br />

i = 1.99;<br />

j = -1.99;<br />

printf("i = %d; j = %d\n", i, j);<br />

return 0;<br />

}<br />

This program produces the result "i = 1; j = -1". Note that the floating point numbers have been truncated and not<br />

rounded.<br />

When converting integers to floating-point, be aware that a "float" has fewer digits <strong>of</strong> precision than an "int", even<br />

though they both use 4 bytes <strong>of</strong> storage (on normal PCs). This can result in some strange behaviour, e.g. (convert2.c),<br />

#include <br />

int main(void) {<br />

unsigned int i;<br />

float f;<br />

i = 4294967295; /* the largest unsigned int */<br />

f = i; /* convert it to a float */<br />

printf("%u %20.13e %20.13e\n", i, f, f - i);<br />

return 0;<br />

}<br />

This program produces the following output when compiled with "gcc":<br />

4294967295 4.2949672960000e+09 1.0000000000000e+00<br />

Rather than rely on automatic type-conversion, you can be explicit about it by using a type-cast operator, e.g.,<br />

f = (float)i;<br />

This converts the number or variable or parethesised expression immediately to its right, to the indicated type. It is a<br />

good idea to use type-casting to ensure that you leave nothing to chance.<br />

Operators<br />

C has a rich set <strong>of</strong> operators (i.e., things like + - * /), which allow you to write complicated expressions quite<br />

compactly.<br />

Unary operators

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

Saved successfully!

Ooh no, something went wrong!