Variables and Arithmetic¶
My Second Program (1)¶
#include <stdio.h>
/* Fahrenheit/Celsius Table
0 - 300, step 20 */
int main(void)
{
int fahr, celsius;
int lower = 0, upper = 300, step = 20;
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
return 0;
}
My Second Program (2)¶
/* ... */
|
Comment (can span multiple lines) |
int fahr, celsius;
|
|
int lower = 0, upper = 300, step = 20;
|
Variable definition and initialization |
My Second Program (3)¶
while (fahr <= upper) {
...
}
|
|
celsius = 5 * (fahr - 32) / 9;
|
Usual arithmetic (expression) ⟶ usual operator precedence rules |
Careful: integer division brutally truncates decimal places!
More natural but always 0:
5/9 * (fahr-32)
My Second Program (4)¶
printf("%d\t%d\n", fahr, celsius);
Formatted output
⟶ number of arguments can vary (?)
“
%d
” obviously means “integer”Important:
printf()
is not part of the core language, but rather an ordinary library function⟶ standard library
More Datatypes¶
|
Integer, nowadays mostly 32 bits wide |
|
Floating point number, mostly 32 bit |
|
Single character (one byte, generally) |
|
Smaller integer |
|
double precision variant of |
Width and precision of all datatypes is machine dependent!
Compound datatypes: arrays, structures, … (⟶ later)