C programming notes - School of Physics
C programming notes - School of Physics
C programming notes - School of Physics
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 />
24 <strong>of</strong> 40 19/03/2007 10:06 AM<br />
}<br />
break;<br />
default: printf("i was outside the range 0 to 2\n");<br />
Here is an example with multiple switch values (switch.c)<br />
Assert as a tool for improving program reliability<br />
By now you will be aware that it is very easy to write buggy programs in C, and it may not always be obvious at<br />
runtime that there is a problem. Checking for all possible error conditions in a program can be tedious. A useful tool to<br />
improve program reliability is the "assert" construct defined in the system include-file "assert.h". An example<br />
(assertexample.c) will clarify this:<br />
#include <br />
#include <br />
int main(void) {<br />
int i;<br />
}<br />
printf("enter a positive integer > ");<br />
assert(1 == scanf("%i", &i));<br />
assert(i > 0);<br />
printf("you successfully entered %i\n", i);<br />
return 0;<br />
If the expression inside parentheses after "assert" is true, the program continues as normal, if the expression is false (i.e,<br />
zero), then the program aborts with an error message.<br />
This may sound like a trivial concept, but by using "assert" extensively, you will be surprised at how your<br />
<strong>programming</strong> improves.<br />
It is particularly useful to use "assert" to check for conditions that should never arise in your program, e.g., suppose you<br />
write a function that should only ever be called with an argument between -1.0 and 1.0, then, use "assert" in the<br />
function to check this, e.g.,<br />
double myAsin(double x) {<br />
assert(fabs(x) < 1.0);<br />
...<br />
}<br />
Also, you can use assert to check for unexpected conditions such as failure to open a file, or failure to allocate memory,<br />
e.g.,<br />
assert(NULL != (fp = fopen("data.dat", "r")));<br />
assert(NULL != (p = malloc(1024));<br />
Formatted output: printf description<br />
The format string given to the "printf" function may contain both ordinary characters (which are simply printed out)<br />
and conversion characters (beginning with a percent symbol, %, these define how the value <strong>of</strong> an internal variable is to<br />
be converted into a character string for output).<br />
Here is the syntax <strong>of</strong> a conversion specification:<br />
%{flags: - + space 0 #}{minimum field width}{.}{precision}{length modifier}{conversion character}<br />
Flags: "-" means left-justify (default is right-justify), "+" means that a sign will always be used, " " prefix a<br />
space, "0" pad to the field width with zeroes, "#" specifies an alternate form (for details, see the manual!). Flags<br />
can be concatenated in any order, or left <strong>of</strong>f altogether.