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;
  • Variable definition

  • Must come at the beginning of a block

int lower = 0, upper = 300, step = 20;

Variable definition and initialization

My Second Program (3)#

while (fahr <= upper) {
    ...
}
  • Loop: “While condition holds, execute body

  • Condition: fahr is less or equal 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#

int

Integer, nowadays mostly 32 bits wide

float

Floating point number, mostly 32 bit

char

Single character (one byte, generally)

short

Smaller integer

double

double precision variant of float

  • Width and precision of all datatypes is machine dependent!

  • Compound datatypes: arrays, structures, … (⟶ later)