teaching machines

CS 330 Lecture 11 – Hi, C

February 15, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

Code

first.c

#include <stdio.h>

// ----------------------------------------------------------------------------
// Function declarations
// ----------------------------------------------------------------------------
void clamp(int *number);
void clampByValue(int number);

// ----------------------------------------------------------------------------
// Function implementations
// ----------------------------------------------------------------------------
int main(int argc, char **argv) {
  double d;
  float f;

  char c;
  int i = 67;
  short int s;
  long int l;

  /* unsigned char c; */
  /* unsigned int i = 67; */
  /* unsigned short int s; */
  /* unsigned long int l; */

  printf("int: %d\n", sizeof(int));
  printf("double: %d\n", sizeof(double));
  printf("float: %d\n", sizeof(float));
  printf("char: %d\n", sizeof(char));
  printf("short int: %d\n", sizeof(short int));
  printf("long int: %d\n", sizeof(long int));
  printf("long long int: %d\n", sizeof(long long int));

  printf("Hey, user. Gimme two numbers: ");
  int a;
  int b;
  scanf("%d %d", &a, &b);
  printf("a: %d\n", a);
  printf("b: %d\n", b);

  /* clamp(&a); */
  /* clamp(&b); */
  clampByValue(a);
  clampByValue(b);

  printf("a: %d\n", a);
  printf("b: %d\n", b);

  return 17;
}

void clamp(int *number) {
  if (*number < 0) {
    *number = 0;
  } else if (*number > 100) {
    *number = 100;
  }
}

void clampByValue(int number) {
  if (number < 0) {
    number = 0;
  } else if (number > 100) {
    number = 100;
  }
}

Haiku

Goodbye, exceptions!
Hello, segmentation faults–
We can only hope.