CS 136 - Week 3

Class 5 - January 20, 2025

Side effects

Testing

Reading input

int x = 0;
int retval = scanf("%d", &x)
// Add files to test this program.
// Do not modify main.c in any way.
#include "cs136.h"
int main(void) {
  int val = 0;
  int retval = 0;
  retval = scanf("%d", &val);
  if (1 == retval) {
    printf("Read an int: %d\n", val);
  } else {
    printf("Failed to read: val is still %d\n", val);
    printf("retval is %d\n", retval);
  }
}

This is a stream of input:

2 4 6 0 1

scanf reads one character at a time. There's no way for it to go back. After it's read one thing, the paper is essentially shredded. Think of the long stream of tape

WHAT THE HELL IS GOING ON HERE

Class 6 - January 22, 2025