COMP1511 - Programming Fundamentals Lecture Notes - COMP1511_ Programming Fundamentals Using a - Studocu
COMP1511 - Programming Fundamentals Lecture Notes - COMP1511_ Programming Fundamentals Using a - Studocu
Using a Terminal
+ The main interface to Linux is a terminal where all our interaction is in text
+ Some commands include:
o ls
Lists all the files in the current directory
o mkdir directoryName
Makes a new directory called directoryName
o cd
Changes the current directory
o pwd
Tells you where you are in the directory structure at the moment
+ gedit
o A basic text editor
o Helps out a little by highlighting C in different colours
+ dcc/gcc
o A compiler – A translator that takes our formal human readable C and turns it into
the actual machine-readable program
o The result of the compiler is something we can “run”
Programming in C
+ C is:
o A clear language with defined rules so that nothing we write in it is ambiguous
o Many modern programming languages are based on C
o A good starting point for learning how to control a computer from its roots
+ Comments
o Put into our code to communicate with others viewing the code
o // in front of a line makes it a comment
o Everything between /* and */ will be comments
o The compiler will ignore comments, so they don’t have to be proper code
o Generally, we will sign off with our name and date for future reference
+ #include
o Asks the compiler to grab another file of code and add it to ours
o stdio.h is the Standard Input Output Library, allowing us to make text appear on the
screen
o the ‘.h’ at the end distinguishes different files which in this case signifies that it is the
header file
+ The “main” Function
o A function is a block of code that is a set of instructions
o Our computer will run this code line by line, executing our instructions
o The first line tells us:
int is the output – this stands for integer, which is a whole number
main is the name of the function
(void) means that this function doesn’t take any input
+ Variables
o Our way of asking the computer to remember something for us
o Called a ‘variable’ as its value can be changed
o It is a certain number of bits that we use to represent something
o Two different types of variables:
int – integer, a whole number (e.g. 0, 1, 2, 3)
double – floating point number (e.g. 3.14159, 8.534, 7.11)
+ Naming Variables
o names are a quick description of what the variable is
e.g. “answer” and “diameter”
rather than “a” and “b”
names are usually nouns and we avoid single letter variables as they can
easily become confusing
o we always use lower case letters to start our variable names
o C is case sensitive:
“ansWer” and “answer” are two different variables
o C also reserves some words
“return”, “int” and “double” can’t be used as variable names
o multiple words
words can be split with underscores or capitals: “long_answer” or
“shortAnswer”
+ int
oa whole number
omost commonly uses 32 bits (which is also 4 bytes)
othis gives us exactly 232 different possible values
othis also means the integer is limited, it can’t just hold any value
most of what we will be doing won’t hit the limits
o ranges from -2147483648 to 21474843647
o Issues:
Overflow and Underflow
If we add two large ints together, we might go over the maximum value,
which will actually roll around the minimum value and possibly end up
negative
Hence we need to be aware of the limits
Ints might not always be 32 bits, this is dependent on the OS
+ double
o A double-sized floating point number
o A decimal value – “floating point” means the point can be anywhere in the number
E.g. 10.567 or 105.67 (the points are in different places in the same digits)
o It’s called “double” because it’s usually 64 bits, hence the double size of our integers
o Issues:
No such thing as infinite precision
We can’t precisely encode a simple number like 1/3
Floats will run out of digits
If we divide 1.0 by 3.0, we’ll get an approximation of 1/3
This means that a lot of doubles are approximate numbers
The effect of approximation can compound the more you use them
o printf
Not just for specific messages we type in advance
We can also print variables to our display
the variables will match the symbols in the same order as they appear
o printing different types of variables
+ Constants
o sometimes we will use fixed numbers using a special command, #define
o we name them in all caps so that we remember that they’re not variables
o NB: if typing xPI without any spaces, PI will not be replaced with the number
o Defined above main function
o In this case, the computer will replace PI with the given number anywhere in the
code
+ Maths
o +, -, *, /
o These will happen in their normal mathematical order
File Operations
o cp source destination – copy
o mv source destination – move (can also be used to rename)
o rm filename – remove a file (delete)
o the -r tag can be added to cp or rm to recursively go through a directory and
perform the command on all the files -> within that specific directory
o cd.. -> goes back once in directories
o mv printDemo2.c idontwantthishere.c to change the name of printDemo2.c to
idontwantthishere.c
The if statement
o an if is like a question and an answer
o first we ask a question
o if we get the right answer, we run some code
If the answer is wrong then the code will not run
+ While Loops
Every time we get to the bottom, the counter increases by one until it
equals 10 then the program will leave the loop and continue
running the rest of the code
o Termination
It is very easy to make a program that goes forever
Consider the following while loop:
Using an expression that is never false, the loop will never finish
Every time we create a loop, we need to figure out how it is going to
stop to avoid the program never finishing
o Using a Sentinel Variable with While Loops
A sentinel is a variable we use to decide when to exit a while loop
This program will keep running until the user types in an odd number
endLoop becomes 1 when the user types an odd number and thus the
while loop will not run
o While Loops inside While Loops
each time a loop runs
it runs the other loop
the inside loop ends up running a lot of times
+ new C syntax
+ output percentage
+ using doubles
o Result of a division will be a double if one of the variables in it is a double
o We could change one of the variables in our division to a double
o This could be done in the declaration of the variable
o But we can also just do it at the point it is used
Code Style
o Indentation and bracketing
o Names of variables and functions
o Repetition (or not) of code
o Clear comments
o Consistency
+ Naming variables
o Variable names are for humans
o Can you describe what a variable is in a word or two?
o If your lab partner was to read this name, would it make sense?
o Does it distinguish well against the other variables
+ Indentation
o A common convention is to use 4 spaces for indentation
+ Spacing
Functions
o A separate piece of code identified by a name
o It has inputs and an output
o If we “call” a function it will run the code in the function
+ Function Syntax
o We write a function with (in order left to right):
An output (known as the function’s type)
A name
Zero or more input(s) (also known as function parameters)
A body of code in curly brackets
+ Return
o return will deliver the output of a function
o return will also stop the function running and return to where it was called from
o finishing the function that it is in and then delivers the output and stops the function
from running again. Only if it is at the end of the main function, it will stop the whole
code
+ Using functions
o we can use a function by calling it by name
o and providing it with input(s) of the correct types)
o Expecting that output of this code would be 10 due to the add function
o A compiler will process our code, line by line, from top to bottom
o If it has seen something before, it will know its name
o Thus, variables must be declared before it can be used since the compiler does not
know this variable
o Since otherNumber in this case is declared after it is used, the compiler would warn
the user of an error
+ Declaring Functions
o All functions must be declared before they can be used
+ Void Functions
o We can also run functions that return no output
o We can use a void function if we don’t need anything back from it
o The return keyword will be used without a value in a void function
Turing Machines
Originally a theoretical idea of computation
o There is a tape that can be infinitely long
o We have a “head” that can read or write to this tape
Can write a 0 or a 1
o We can move the head along to any part of the tape
o There’s a “state” in which the machine remembers its current status
The memory component
o There’s a set of instructions that say what to do in each state
o Theoretically will never run out of memory (unlike our computers which have limited
storage)
o Everything that our computer can do, this machine can do (given the necessary
amount of time)
+ The Processor
o Known as Central Processing Units (CPUs)
Maintains a “state”
Works based on a current set of instructions
Can read and write from/to memory
Basically the brain of the computer
o In C:
State – where are we up to in the code right now
Instructions – compiled from our lines of code
Reading/Writing – Variables
+ Memory
o From registers (tiny bits of memory on the CPU) through Random Access Memory
(RAM) and to the Hard Disk Drive. All of these are used to store information
o How does C use memory?
On the Hard Drive
Our C source code files are stored on our Hard Drive
dcc compiles our source into another file, the executable program
In Random Access memory
When we run our program, all the instructions are copied into RAM
Our CPU will work through memory executing our instructions in order
Our variables are stored in RAM as well
Reading and writing to variables will change the numbers in
RAM
o What happens in memory when we run a program?
Our Operating System gives us a chunk of memory
Our program copies its instructions there
Some space is reserved for declared variables
The Stack is used to track the current state
The stack grows and shrinks as the program runs
The Heap is empty and ready for use
We can use the heap to store data while the program is running
Arrays
o Using arrays, we will be able to access integers as a group and loop
through and access each individual element
+ What is an array?
o A variable is a small amount of memory
o An array is a larger amount of memory that contains multiple variables
o All of the elements (individual variables) in an array are the same type
o Individual elements don’t get names, they are accessed by an integer index
+ Declaring an Array
o int – the type of the variables stored in the array
o [10] – the number of elements in the array
So in this case we will be using 320 bits of memory for our array since there will
be 10 integers
o – {0} – initialises the array as all zeroes
Can only do this with 0, cannot do this with any other number
o – {0,1,2,3,4,5,6,7,8,9} – initialises the array with these values
+ Array Elements
o An element is a single variable inside the array
o They are accessed by their index, an int that is like their address
o Indexes start from 0
Basically tells us how far into the array we need to go to access the number we
want
We start from 0 as we do not have to move along to get the first element. To
access the second element we ‘step in’ once
o Trying to access an index outside of the array will cause errors
In this example, element 2 of arrayOfMarks is 44 and element 6 is 62
+ Accessing elements in C
+ New Syntax
o += is used for accumulating values in a variable
+ Input into an array
o We can’t access the whole array, only individual elements
o But we can loop through the array entering values
Memory
o Let’s start with an idea of a neighbourhood
o Each house is a piece of memory (a byte)
o Every house has a unique address that we can use to find it
o We could think of our entire computer’s memory as a big array of bytes
+ A neighbourhood of memory
o Every block of memory has an address
The address is actually an integer
o Houses and addresses
A variable is a house
the house is in a certain location in memory
the house contains the bits and bytes that decide what the value of the
variable is
the address is an integer
in a 64 bit system, we’ll usually use a 64 bit integer to store an address
we can address 264 bytes of memory
Pointers
o variables that hold memory addresses
o created to point at the location of variables
o if a variable was a house, the pointer would be the address of that house
o pointers are usually created with the intention of “aiming at” a variable (storing a
particular variable’s address)
+ Pointers in C
o use a * to declare a variable as a pointer
o a pointer is most often “aimed” at a particular variable
o that means the pointer stores the address of that variable
o we use & to find the address of a variable
+ Initialising Pointers
o Should be initialised like other variables
o Generally, pointers will be initialised by pointing at a variable
o “NULL” is a #define from most standard C libraries (including stdio.h)
o If we need to initialise a pointer that is not aimed at anything, we will use NULL
+ Using Pointers
o Looking at the variable that a pointer “points at”
We can use * on a pointer to access (dereference) the variable it points at
Using the address analogy, this is like asking what’s inside the house at that
address
%p in printf will print the address stored in a pointer
ip is the address of the integer while *ip is the actual integer itself
to go from integers to pointers we use & and to go from pointers to integers we
use *
o In this case, the copy of the variable can’t every change the value of the variable,
because it is just a copy
+ Pointers also pass in “by value”
o The equals means ‘take this and put it into that’ so for the last line, we are putting
the value of temp into *num2
Debugging
o The process of finding and removing software bugs
o Errors in code are called ‘bugs’
Something we have written (or not written) in our code
Any kind of error that stops the program from running as intended
Includes syntax and logical errors
+ Syntax Errors
o C is more specific than most human languages
o Slight mistakes in the characters we use can result in a different behaviour
o Some syntax errors are obvious, and your compiler will find them
o Some are more devious, and the error message will be the consequence of the bug,
rather than the bug itself
+ Logical Errors
o We can write a functional program that still doesn’t solve our problem
o Logical errors can be syntactically correct
o But the program might not do what we intended
sometimes we read the problem specification wrongly
sometimes we forget the initial goal of the program
sometimes we solve the wrong problem
sometimes we forget how the program might be used
+ Finding bugs
o compilers can sometimes catch some syntactical bugs
the compiler can be trusted to understand the language better than us
the simplest thing we can do is run dcc and see what happens
if dcc runs and we don’t get any syntax errors, there might still be
errors within and get errors later on when we run the program
errors found by the compiler are known as ‘compiler-time errors’
when dcc gives errors and warnings
always start with the first error
subsequent errors might just be a consequence of that first line
an error is the result of an issue, not necessarily the cause
at the very least, you will know a line and character where something
has gone wrong
warning says it is possible to compile but there might be a problem
when you end up running the program
o we need to learn how to use compilers to correct our code
look for a line number and character (column) in the error message
sometimes knowing where it is is enough for you to solve it
read the error message and try to interpret it
remember that the error message is from a program that reads code
it might not make sense initially
sometimes it’s an expectation of something that’s missing
sometimes it’s confusion based on something being incorrect syntax
sometimes after fixing just one bug in the code, we might be moving the error
somewhere else and end up with more error messages
o code reviews and pair programming help for logical bugs
discussing different ways of doing things and shortening our code
easier to spot our own logical errors
o testing is always important
run the code
try different types of inputs to see different situations (autotest)
try using inputs that are not what is expected
use output to show information as the program runs
check different sections of the code to see where errors are
+ Characters
o new type of variable called char
o characters are what we think of as letters , like ‘a’, ‘b’, ‘c’ etc
o they can also represent numbers, like ‘0’, ‘1’, ‘2’ etc
o they are actually 8 bit integers
o we use them as characters, but they’re actually encoded numbers
o ASCII (American Standard Code for Information Interchange)
o We will not be using char for individual characters, but we will in arrays
o ASCII specifically uses values 0-127 and encodes
Upper and lower case English letters
Digits 0-9
Punctuation symbols
Space and newline
o It is not necessary to memorise ASCII, rather it’s important to remember that
characters can be treated like numbers sometimes
o Use of %c in the printf will format the variable as a character
+ Invisible Characters
o there are other ASCII codes for “characters” that can’t be seen
o there is no visible character on terminal when we run the program
o newline (\n) is a character
o space is a character
o there’s also a special character, EOF (End of File) that signifies that there’s no more
input
o EOF has been #defined in stdio.h, so we use it like a constant
o We can signal the end of input in a Linux terminal by using Ctrl-D
Strings
o when we have multiple characters together, we call it a string
o strings in C are arrays of char variables containing ASCII code
o strings are basically words, while chars are letters
o strings have a helping element at the end, a ‘\0’
o it’s often called the ‘null terminator’ and it is an invisible character
o this helps us know if we’re at the end of the string
everything after is not read by the computer
+ strings in Code
o strings are arrays of type char, but they have a convenient shorthand
Structs
o A new way of collecting variables together
o Structs(short for structures) are a way to create custom variables
o Structs are variable that are made up of other variables
o There are not limited to a single type like arrays
o They are also able to name their variables
o Structs are creating our own variable type
We need to declare this type before any of the functions that use it
We declare what a struct is called and what the fields(variables) are
+ Structs as Variables
o Arrays of structs are possible
o Structs can be some of the variables inside other structs
o Once a struct has been defined, it can be used like any other variable
Memory
+ Functions and Memory
o Everything gets passed ‘by value’
o Variables are copied by the function
o The function will then work with their own versions of the variables
+ Memory in Functions
+ Memory Allocation
+ Cleaning up
Multi-File Projects
o we can #include our own files, allowing us to join projects together
o we will often make some code that we can use again
o if we make it in its own file, with its own interface we can #include it in our projects
+ File.h
o Header Files show you what the code’s functions are
o This file shows a programmer all they need to know to use our code
o typedef (Type Define) is a way of allowing us to create our own C Type out of
another Type
o this protects our struct from access and keeps our data safe
o Function Declarations with no definitions
o Comments that describe how the functions can be used
o No running code
+ File.c
o Implementation Files show you how the code runs in detail
o We can hide the complicated running code in this file
o Has includes, especially #include “File.h” (joins the two files together)
o If we have the file we use “ “ but when it is a file within the compiler like our
standard libraries we use <>
o Implements the struct mentioned in the typedef from the header
o Implements all the functions declared in the header
o This extra text we type after the name of our program can be passed into our
program as strings
Linked Lists
o not one continuous block of memory
o items can be shuffled around by changing where pointers aim
o length is not fixed when created
o you can add or remove items from inside the list
o the 2nd node points its “next” at the old head, then it replaces head with its own
address
o we can use Beat as a Type without knowing anything about the struct underlying it
o in a Header file
we can put this in a header (*.h) file along with functions that use it
allows someone to see a Type without knowing exactly what it is
the details go in the *.c file which is not included directly
can see the functions without knowing how they work
able to see the header and use the information
hide the implementation that we don’t need to know about
+ Queue
loop through all the elements until the next pointer is NULL
add something to the end, pointing the NULL pointer at the new node
we could keep track of the last element in the list using a struct
connect the new object to the current tail
move the tail pointer to the new last object
+ Stacks
o a ‘last in first out’ structure
o can put something on the top of a stack and take something off the top of a stack
o anything underneath cannot be accessed
o similar to how functions run
o Implementation
It can be a linked list or an array
Should be invisible to whoever includes the stack.h file
Keep track of where the top is with an int
o Array implementation
Data is stored in an array with a large maximum size
The top is a particular index which signifies where the data ends and is the
number of elements in the stack
Pop should return the element on the top of the stack and then move the
top index down one
o Linked list implementation
Add elements to the end
Remove elements from the same end
Push and Pop
Push adds an element to the top of the list and top will then point at that
element
Pop removes the top element and returns it then point at the next
element