Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
C Programming – Basics<br />
| expr3;<br />
| }<br />
exprl, expr2 and expr3 may be as complicated as you like<br />
do<br />
statement<br />
while (expression)<br />
This is a post-tested loop which continues to execute while expression is non-zero.<br />
! Multi-way Decision<br />
switch (expression)<br />
{<br />
case optionl : statement list<br />
case option2 : statement list<br />
...<br />
case optionN : statement list<br />
default: statement list<br />
}<br />
Notes:<br />
- similar to 'case' in Pascal<br />
- execution starts at <strong>the</strong> statement list which has a label corresponding to <strong>the</strong> value of <strong>the</strong> expression and<br />
continues through all remaining statement lists<br />
To stop processing in <strong>the</strong> middle of a switch statement, a break statement can be used to terminate execution of<br />
<strong>the</strong> current block.<br />
Functions Returning Non-integers<br />
Often functions return (void) or int<br />
Many functions like sqrt, sin, and cos return double<br />
#include <br />
double atof(char s[]) /* atof: convert string s to double */<br />
{<br />
double val, power;<br />
int i, sign;<br />
for (i=0; isspace(s[i]); i++); /* skip white space */<br />
sign = (s[i] == '-') ? -1 : 1;<br />
if (s[i] == '+' || s[i] == '-') i++;<br />
for (val=0.0; isdigit(s[i]); i++)<br />
val = 10.0*val + (s[i] - '0');<br />
if (s[i] == '.') i++;<br />
for (power=1.0; isdigit(s[i]); i++) {<br />
val = 10.0*val + (s[i] - '0');<br />
power *= 10.0;<br />
}<br />
return sign*val/power;<br />
}<br />
The calling routine must know what atof returns thus all functions should be explicitly declared.<br />
#include <br />
#define MAXLlNE 100<br />
main() /* simple calculator */<br />
{<br />
double sum, atof(char []);<br />
char line[MAXLlNE];<br />
int getline{char line[], int max);<br />
sum = 0;<br />
while (getline{line, MAXLlNE) > 0)<br />
printf{"\t%g\n", sum += atof(line));<br />
return 0;<br />
}<br />
29