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 andcstring
functionality.#include <math.h>
Standard library for extra math functions beyond basic arithmetic.
Important: You need to compile with-lm
if you use themath.h
libary.
2 Input/Output
From #include "si204.h"
:
void writenum(double num, stream destination);
Writesnum
to the output streamdestination
. Note thatnum
could also be anint
.
Example:writenum(42, stdout);
void fputs(cstring output, stream destination);
Writesoutput
to the output streamdestination
.
Example:fputs("Hello, world!\n", stdout);
void fputc(char c, stream destination);
Write the single characterc
to the output streamdestination
.
Example:fputc('!', stdout);
double readnum(stream source);
Reads the next number (after skipping whitespace) from the input streamsource
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 streamsource
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 streamsource
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, astream
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 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 ofsrc
todest
. The value ofdest
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 stringsa
andb
according to alphabetic (lexicographical) order, and returns a negative number ifa
is less thanb
,0
ifa
is equal tob
, or a positive number ifa
is 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 7
void strcat(cstring dest, cstring src);
Appends the second stringsrc
onto the end ofdest
, modifyingdest
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 ofx
.
Example:fabs(-1.2); // equals 1.2
double pow(double x, double y);
Returnsx
raised to the powery
, i.e., \(x^y\)
Example:pow(2.0, 3.0); // equals 8.0
double sqrt(double x);
Returns the square root ofx
.
Example:sqrt(4.84); // equals 2.2
double log(double x);
Returns the natural logarithm (meaning log base e) ofx
.
Example:log(45.6); // equals 3.8199...