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 cppreference.com/w/c, 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 andcstringfunctionality.#include <math.h>
Standard library for extra math functions beyond basic arithmetic.
Important: You need to compile with-lmif you use themath.hlibary.
2 Input/Output
From #include "si204.h":
void writenum(double num, stream destination);
Writesnumto the output streamdestination. Note thatnumcould also be anint.
Example:writenum(42, stdout);void fputs(cstring output, stream destination);
Writesoutputto the output streamdestination.
Example:fputs("Hello, world!\n", stdout);void fputc(char c, stream destination);
Write the single charactercto the output streamdestination.
Example:fputc('!', stdout);double readnum(stream source);
Reads the next number (after skipping whitespace) from the input streamsourceand 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 streamsourceand returns it.
Example:char c = readchar(stdin);void readstring(cstring dest, stream source);
Reads the next group of non-whitespace characters from the input streamsourceand 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, astreamobject 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 tofopen, 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 ofsrctodest. The value ofdestis 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 stringsaandbaccording to alphabetic (lexicographical) order, and returns a negative number ifais less thanb,0ifais equal tob, or a positive number ifais greater thanb.
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 strings.
Example:int x = strlen("My bike"); // x will equal 7void strcat(cstring dest, cstring src);
Appends the second stringsrconto the end ofdest, modifyingdestto 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 ofx.
Example:fabs(-1.2); // equals 1.2double pow(double x, double y);
Returnsxraised to the powery, i.e., \(x^y\)
Example:pow(2.0, 3.0); // equals 8.0double sqrt(double x);
Returns the square root ofx.
Example:sqrt(4.84); // equals 2.2double log(double x);
Returns the natural logarithm (meaning log base e) ofx.
Example:log(45.6); // equals 3.8199...