Apr 14, 2023COMP1511 CheatsheetThis COMP1511 Cheatsheet should provide you with a quick overview of the 1511 course and its key concepts.
Saga Chandra8 min read

Overview

This COMP1511 Cheatsheet should provide you with a quick overview of the 1511 course and its key concepts. This won't cover all the possible edgecases, and any further questions should refer to the C documetation, or other people.

Basic Syntax

These contain the basic syntax of C.

ElementC SyntaxExplanation
Definitiontype name;Provides a definition for a variable
Function Prototypereturn_type name(parameters);Provides a prototype for a function
Function Definitionreturn_type name(parameters){};Provides the definition for a function.
If Statementif (expression) {}Evaluates your expression and executes the code block if true
If Else Statementelse if (expression) {}Requires a previous if statement, will act similarly to an if statement
Else Statementelse (expression) {}Requires a previous if statement, will execute when the if statement is false
While Statementwhile (expression) {}Will continue to execute the codeblock as long as expression is true
For Loopfor (init; cond; iter) {}Will initialise an expression evaluated once (init), evaluate a conditional before the loop body, and then evaluate the iter statement after the loop body
Return Statementreturn (expression)Evaluates the expression, terminates the current function, and returns the evaluated expression
Pointertype *name;A pointer that points to a given type. Remember that a pointer contains a memory address

Types

A list of your basic C types.

ElementExplanation
intA 32 bit integer
charAn 8 bit ASCII character
long longA 64 bit integer
doubleA 64 bit floating-point

Operators

Basic

ElementExplanation
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
&&AND
!NOT
==EQUAL

Bitwise (For those who enjoy pain)

ElementExplanation
&bitwise AND
|bitwise OR
^bitwise XOR
<<left bitshift
>>right bitshift
~bitwise NOT

I/O (Input/Output)

Most I/O functions are defined under the header stdio.h.

ElementC SyntaxExplanation
fgetsfgets(char *dest, int n, FILE *stream)fgets will read bytes from a stream (STDIN), into an array pointed to by dest, reading until either of 3 conditions are fulfilled. n-1 characters are read, a \n character is read, or the end of stream.
printfprintf(char *format, ...)printf formats and writes the string pointed by format to standard output (Prints to console)
scanfscanf(char *format, ...)scanf reads data from stdin and stores them in locations given by the additional specifiers

Format Specifications

Format Specifiers for use in your printf() and scanf() functions.

ElementExplanation
dSigned Decimal Integer
fDecimal Floating Point
cCharacter
sString

Data Structures

A list of basic data structures that we have learnt over the course of COMP1511.

ElementC SyntaxExplanation
Enumenum {};Enums define a 'collection' of keywords. Under the hood, they are simply just integers.
Arraytype name[size]A contiguous (adjacent) space of memory that allows us to 'associate' values together. Remember that they can be chained to form n-th dimensional arrays.
Structsstruct name {};A container that is able to hold varying types unlike arrays. They generally have to be instantiated to be used

Linked Lists

Linked lists are a special form of data structures in that they are not implicitly defined in any C standard library. The main advantage of linked lists over arrays are that they are dynamic and their size can be changed at runtime (they can be changed after you compile the program) unlike arrays, where their size must be defined at compile-time.

int arr[300];
// Here the compiler must know the size of the array before it is compiled.

Note: There is an exception to this, it is called a variable-length array and is supported since C99. However, these should not be used.

void foo(int n) {
	int arr[n];
	// This is a VLA and should not be used.
}

Defining our Nodes

struct Node {
	int data; // holds the information stored in our node
	struct node *next; // connects our nodes together
}

Remember that Linked Lists are (usually) not stored contiguously in memory, meaning that they are usually at random locations throughout memory. Therefore, we will need to use pointers to connect our scattered nodes together.

struct Node *head = malloc(sizeof(struct Node));
head->data = 3;
head->next = malloc(sizeof(struct Node));
head->next->next = NULL;
head->next->data = 2;
// This is equivalent to 3 -> 2 -> NULL

Standard Libraries

Some useful standard library headers.

<string.h>

ElementC SyntaxExplanationReturns
memcpyvoid *memcpy(void *destination, void *src, size_t n)Takes in any two pointers, dest and src and copies n bytes from the location at src and copies it onto the location at destdest
strcpychar *strcpy(char *dest, char *src)Copies the string pointed by src into the location at dest. including the null terminator (\0)dest
strcatchar *strcat(char *dest, char *src)Appends a copy of src onto the end of dest. The null terminator is overwritten.dest
memcmpint memcmp(void *p1, void *p2, size_t n)Takes in any two pointers and compares the first n bytes0 - If they are the same
strcmpint strcmp(char *s1, char *s2)Takes in two strings and compares the two, returning 0 if they are the same0 - If they are the same
strtokchar *strtok(char *s, char *delim)Splits your string into multiple strings separated by the given delimiterIf a token is found, a pointer to the beginning of the token
strlensize_t strlen(char *s)Gives you the length of your stringReturns the length of your string determined by \0.

<math.h>

ElementC SyntaxExplanationReturns
powdouble pow(double base, double exponent)Finds your base raised to your exponentReturns your base raised to your exponent
sqrtdouble sqrt(double num)Square roots your numSquare root of num
ceildouble ceil(double num)Rounds upRounded up num
floordouble floor(double num)Rounds downRounded down num

<stdlib.h>

ElementC SyntaxExplanationReturns
atoiint atoi(char *s)Converts ASCII to an integer. i.e, "32" -> 32Returns an integer
freevoid free(void *p)A block of memory previously allocated by malloc() is freedNothing
mallocvoid *malloc(size_t n)Allocates n bytes of memoryReturns a pointer to the start of the block

<ctype.h>

ElementC SyntaxExplanationReturns
isalphaint isalpha(int n)Checks if n is an alphabetic letterA non-zero integer if true, 0 if false
isdigitint isdigit(int n)Checks if n is a decimal numberA non-zero integer if true, 0 if false
islowerint islower(int n)Checks if n is a lower case alphabetic letterA non-zero integer if true, 0 if false
isupperint isupper(int n)Checks if n is a upper case alphabetic letterA non-zero integer if true, 0 if false
tolowerint tolower(int n)Converts n to a lower case letter if it is upper case and has a lower case equivalentReturns n - remains unchanged if conditions not fulfilled
toupperint toupper(int n)Converts n to an upper case letter if it is lower case and has an upper case equivalentReturns n - remains unchanged if conditions not fulfilled