SI 204 Spring 2017 / Resources

This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

C reference

This page will be updated throughout the semester with quick reference for the most important C language functions that will be needed for the class. Most external links are to, which is an excellent general reference on the C language and standard libraries.

1 Header files

  • #include "si204.h"
    Header file for SI204 that includes basic I/O and cstring functionality.
  • #include <math.h>
    Standard library for extra math functions beyond basic arithmetic.
    Important: You need to compile with -lm if you use the math.h libary.

2 Input/Output

From #include "si204.h":

  • void writenum(double num, stream destination);
    Writes num to the output stream destination. Note that num could also be an int.
    Example: writenum(42, stdout);
  • void fputs(cstring output, stream destination);
    Writes output to the output stream destination.
    Example: fputs("Hello, world!\n", stdout);
  • void fputc(char c, stream destination);
    Write the single character c to the output stream destination.
    Example: fputc('!', stdout);
  • double readnum(stream source);
    Reads the next number (after skipping whitespace) from the input stream source and returns it. Note that this can be used to read integers also. If the next part of input is not an input, and error occurs.
    Example: int x = readnum(stdin);
  • char readchar(stream source);
    Reads the next non-whitespace character from the input stream source and returns it.
    Example: char c = readchar(stdin);
  • void readstring(cstring dest, stream source);
    Reads the next group of non-whitespace characters from the input stream source and saves them to the variable passed as the first argument.
    Example: readstring(mystring, stdin);
  • stream fopen(cstring filename, "r");
    Opens the given file as an input stream for reading. If the file doesn't exist, a stream object is returned that is equal to 0 (i.e., false).
    Example: stream fin = fopen("file.txt", "r");
  • void fclose(stream inorout);
    Closes the given file stream. You should call this after any successful call to fopen, once you're done using the stream.
    Example: fclose(fin);

3 String Operations

From #include "si204.h":

  • cstring strcpy(cstring dest, cstring src);
    Copies (assigns) the value of src to dest. The value of dest is also returned for convenience, but you usually don't use the return value.
    Example: strcpy(mystring, "words of wisdom");
  • int strcmp(cstring a, cstring b);
    Compares strings a and b according to alphabetic (lexicographical) order, and returns a negative number if a is less than b, 0 if a is equal to b, or a positive number if a is greater than b.
    Example 1: int x = strcmp("red", "house"); // x will be positive
    Example 2: if (strcmp(name, "Dan") == 0) { fputs("Hi, Dan!\n", stdout); }
  • int strlen(cstring s);
    Returns the number of characters (i.e., the length) of the string s.
    Example: int x = strlen("My bike"); // x will equal 7
  • void strcat(cstring dest, cstring src);
    Appends the second string src onto the end of dest, modifying dest to make it longer. For example:
    strcpy(mystring, "First");  // now mystring equals "First"
    strcat(mystring, "Second"); // now mystring equals "FirstSecond"

4 Math functions

From #include <math.h> (don't forget to compile with -lm):

  • double fabs(double x);
    Returns the absolute value of x.
    Example: fabs(-1.2); // equals 1.2
  • double pow(double x, double y);
    Returns x raised to the power y, i.e., \(x^y\)
    Example: pow(2.0, 3.0); // equals 8.0
  • double sqrt(double x);
    Returns the square root of x.
    Example: sqrt(4.84); // equals 2.2
  • double log(double x);
    Returns the natural logarithm (meaning log base e) of x.
    Example: log(45.6); // equals 3.8199...