0% found this document useful (0 votes)
7 views223 pages

Week 1

The document outlines the first week of the CS5008/9 course at Northeastern University's Khoury College, focusing on data structures and algorithms. It covers essential topics such as compiling and running C programs, introductions to hardware and operating systems, and the use of cloud servers for development. Additionally, it provides guidance on connecting to the Khoury Cloud, using text editors, and getting started with GitHub for version management and collaboration.

Uploaded by

jacob999huang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views223 pages

Week 1

The document outlines the first week of the CS5008/9 course at Northeastern University's Khoury College, focusing on data structures and algorithms. It covers essential topics such as compiling and running C programs, introductions to hardware and operating systems, and the use of cloud servers for development. Additionally, it provides guidance on connecting to the Khoury Cloud, using text editors, and getting started with GitHub for version management and collaboration.

Uploaded by

jacob999huang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 223

Graduate Course, Khoury College of Computer Sciences

Northeastern University, [ Silicon Valley] Campus

CS5008/9
Data Structures, Algorithms, and Their Applications Within Computer Systems

Summer 2023 Semester

Professor. Shivakumar Mathapathi

Week 1
Introduction and getting started

Slides reference

Dr. Brent Hailpern


Khoury College of Computer Science, San Jose •


1
cs
Plan for today
How to compile and run a C program
Very quick introduction to hardware
Very quick introduction to operating systems
C basics

2
Plan for today
How to compile and run a C program
Very quick introduction to hardware
Very quick introduction to operating systems
C basics

This lecture is probably the longest of the semester


There is a LOT of information – don’t panic
I do not expect you to memorize stuff

3
A very simplified view of a computer system

Applications

Graphical User Interface (GUI) / Desktop

Language Runtime Environment

Operating System + File System

Hardware Network

4
A very simplified view of a computer system

Applications

Graphical User Interface (GUI) / Desktop

C Runtime Language Runtime Environment

Mac OS or
Linux or Operating System + File System
Windows

Intel or
Hardware Network
Apple

5
A very simplified view of a computer system

Applications

Graphical User Interface (GUI) / Desktop

C Runtime Language Runtime Environment

Mac OS or
Linux or Operating System + File System
Windows

Intel or
Hardware Network
Apple

6
What computer to use?
You can use the Khoury Cloud servers which run Linux
• Much development in companies is done in the cloud on Linux
• You can use this class to learn now how to use cloud servers and github
repositories
• Employers like using cloud servers, because it allows them to provision and
control the development environment

You can use your Mac or Windows laptop

7
If you use the Khoury Cloud servers
You will use your CIS/Khoury username and connect (via ssh) to
[email protected]

If you do not have an Khoury account try


https://my.khoury.northeastern.edu/account/apply
or contact [email protected]

Once you have ssh’d in, you will be running on a Linux machine

8
Connecting to Khoury Cloud
Mac Users

9
Connecting to Khoury Cloud: Mac Users
Step 1: Open a terminal on your computer using
/Applications/Utilities/Terminal
Step 2: Type in the following line and then hit enter
ssh [yourusername]@login.khoury.northeastern.edu

Replace [yourusername] with your CIS/Khoury userid

10
Connecting to Khoury Cloud: Mac Users
• Step 3: Once you hit enter, you will be prompted to enter your
password. As you type your password, nothing will show up in the
terminal which is normal. Hit enter to fully login into the Linux
machine. Once you are fully logged in, you will see the following
screen.

CS 5008 - Fall 2022 - Week 1 11


Connecting to Khoury Cloud
Windows Users

12
Connecting to Khoury Cloud: Windows Users
Step 1: Download the PuTTY terminal emulator here:
https://www.putty.org

13
Connecting to Khoury Cloud: Windows Users
Step 2: Open PuTTY and navigate to configuration settings. Once you
get to the PuTTY configuration settings, enter the following:
• "login.khoury.northeastern.edu" into the Host Name text entry box
• 22 into the Port text entry box
• Check that the connection type is "SSH"
Your PuTTY configurations should look like the following:

14
Connecting to Khoury Cloud: Windows Users
Step 2: Open PuTTY and navigate to configuration settings. Once you
get to the PuTTY configuration settings, enter the following:
• "login.khoury.neu.edu" into the Host Name text entry box
• 22 into the Port text entry box
• Check that the connection type is "SSH"
Your PuTTY configurations should look like the following:

15
Connecting to Khoury Cloud: Windows Users
Step 3: Click open, and it should open a terminal like the following:

16
Connecting to Khoury Cloud: Windows Users
Step 4: Enter your CIS/Khoury username. Once you hit enter, you will
be prompted to enter your CIS/Khoury password. As you type your
password, nothing will show up in the terminal which is normal. Hit
enter to fully login into the Linux machine.

17
Connecting to Khoury Cloud: Windows Users
Once you are fully logged in, you will see a screen like this one.

Done! You have successfully ssh'd into the Linux machine!


18
Caveats
Most of the slides in this presentation will show examples on my Mac
laptop

All of the text editor examples I use are Emacs, but the CS5008 videos
are all done using VIM – I don’t care which text editor you use, but for
this course I strongly suggest you learn one of VIM or Emacs or Nano

Advantages of using the cloud server: gain experience with cloud, all
tool pre-configured for you
Disadvantages of using the cloud server: bandwidth, latency, resources

19
How to use editors
You can google how to use any of the text editors – there is a ton of tutorials
out there
Here is one starting point for each:

Emacs: https://www.linuxfordevices.com/tutorials/linux/emacs-editor-tutorial

Vim: https://www.tutorialspoint.com/vim/index.htm

Nano: https://www.tutorialspoint.com/how-to-use-nano-text-editor

20
What about Integrated Development
Environments (IDEs)?
If you are doing any serious software development, you will use an IDE
– it will help you eliminate silly bugs early and can also help you
integrate your code with code being developed by others.

But...for this class prefer you to work with the command line tools and
basic editors. None of the programs will be too complex and you
should have some grounding in low-level computer tools, in order to
really get an appreciation for what an IDE does for you.

Hopefully, you will be getting good IDE experience in your Java class

21
What about Integrated Development
Environments (IDEs)?
If you are doing any serious software development, you will use an IDE
– it will help you eliminate silly bugs early and can also help you
integrate your code with code being developed by others.
I will not enforce the ”no IDE”
But...for this class I prefer you to work with the command line tools and
recommendation
basic editors. – butwill
None of the programs trust me,complex
be too it will be
and you
should haveworth
some it grounding
for you in
tolow-level
use the computer tools,
basic stuff at in order to
really get an appreciation for what an IDE does for you.
least once in your new career.
Hopefully, you will be getting good IDE experience in your Java class

22
Github

23
Why Github?
Almost every CS-related job you will apply for will use Github or something like Github – learning enough to be
comfortable with Github may be one of the most important skills you will get out of this course – we will only use a
tiny bit of Github for delivering assignments and collecting your homework.
Used correctly, GitHub allows developers to collaborate and work on code for the same application while minimizing
issues with asynchronous work. More than that, open source projects are all managed on sites like Github – and many
employers check to see if you have contributed code to open source projects.

Github is Version management :


• Keep track of changes you made to your code
• Allow different developers to make changes to code and keep versions straight

Github is Replication – a copy on a server and a copy on your laptop

Video – What is Github: https://youtu.be/w3jLJU7DT5E


Video – CS5008 Video - Module 1 – Github
Quick Start Documentation: https://docs.github.com/en/get-started/quickstart/hello-world

24
Getting started with Github
Go to github.com in your browser – if you already have an account then
login, if not sign up for a free account and then login

AFTER you are already logged in, copy the url


https://classroom.github.com/a/tnWTY2ar
into the browser’s address bar and hit enter

25
Getting started with Github

hit accept button

26
Getting started with Github

27
Getting started with Github – refresh browser

you will see your


userid instead of
bhailpern

28
Getting started with Github
You will end up with a repository something like
https://github.com/Northeastern-CS5008-FA22-online/cs5008-fall-2022-bhailpern
(where the last word is YOUR user name and not “bhailpern”!)

Click on your repository URL and then edit the readme.md file in your
repository with your name and email and then commit the change by
hitting the “commit changes” button – this is part of your homework

29
Getting Started with Github

30
Getting Started with Github

You now have a github repository (repo) on the github.com server.


Your homework assignments are already in this repo.

31
Getting necessary tools
Khoury cloud, Mac laptop, Windows laptop

32
Getting necessary tools
Using the Khoury Cloud Servers

33
Khoury cloud servers

If you are going to use the Khoury cloud


You are done – you have all your tools pre-installed!

A big advantage of using the cloud servers is that they are already
provisioned with current versions of the C complier (gcc), git (the
GITHUB access software), and the text editors (emacs and vim)

34
Getting necessary tools
Using a Mac Laptop

35
If you are going to use a Mac laptop
There are two general approaches – both free:

1. use Apple’s Xcode product (via Mac app store)


a. advantages: complete, current version maintained automatically, same
development environment used by real Apple developers
b. disadvantages: takes up a LOT of disk space, could take a long time to download,
you get EVERYTHING (many languages and tools you wont use in this course)

2. download the individual components using Homebrew


a. much smaller than Xcode
b. you are responsible for selecting tools and manually updating them

36
Using a Mac - Xtools

37
Mac using Xcode

C compiler (+other tools) are part of Apple Xcode

you get it from the


Mac App Store

38
Mac using Xcode

The first time you run gcc/vim/emacs/git


in the command shell, you may get popup

39
Mac using Xcode

The first time you run gcc/vim/emacs/git


in the command shell, you may get popup

40
Using a Mac – Homebrew
Homebrew

41
Mac using Homebrew

Homebrew
Macs run a version of Unix (MacOS)
It does not include all the common Unix tools
Homebrew – is the “Missing Package Manager for macOS”
Instructions to install Homebrew are at https://brew.sh

42
Mac using Homebrew

Installing Tools from Homebrew


Once Homebrew is installed, you can then install the other tools you need
from your MacOs command line (terminal app found in
/Applications/Utilities)

brew install gcc


brew install git
brew install vim choose ONE of vim or emacs
brew install emacs

brew update use periodically


brew upgrade

43
Getting necessary tools
Using Windows 10

44
If you are going to use a Windows laptop
There are two general approaches – both free:

1. Download the various components designed for Windows


a. advantages: native tools designed specifically to work with Windows
b. disadvantages: multiple sources to download from

2. Use Linux on Windows


a. Uses commands and tools similar to native Linux and Mac
b. one source for downloading tools

45
Using Windows
Native Tools

46
Windows

Download gcc from www.mingw.org


Download site: https://osdn.net/projects/mingw/releases/

47
Windows

Download gcc from www.mingw.org


Download site: https://osdn.net/projects/mingw/releases/

48
Windows

Minimal configuration is fine

49
Windows

Add to Windows’ path environment variable


see
https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/

On my Windows laptop, I had to add c:\MinGW\bin

50
Windows

Installing git on Windows10


see https://phoenixnap.com/kb/how-to-install-git-windows

51
Windows

Installing emacs on Windows 10


https://www.gnu.org/software/emacs/download.html

52
Windows

Installing vim on Windows


https://www.vim.org/download.php

CS 5008 - Fall 2022 - Week 1 53


Using Windows – WSL (Linux)
See Omar Rashwan’s WSL guide at
https://brent.hailpern.com/wp-content/uploads//2022/05/5008-WSL-Guide.pdf

54
Windows 10 now allows Linux commands
WSL: https://docs.microsoft.com/en-us/windows/wsl/
Install WSL: https://docs.microsoft.com/en-us/windows/wsl/install

55
Getting started with WSL
To install WSL, you must be running Windows 10 or later. Upgrading
Windows is free, so if you are running Windows 9 or earlier please
upgrade first.

Go to https://docs.microsoft.com/en-us/windows/wsl/install
Follow the instructions to install WSL2.

Once WSL is installed, boot it up and run a simple


echo foo
to check that it’s working. You should get a foo back.

56
Installing the tools
Tool package manager:
sudo apt update && sudo apt upgrade

Git
sudo apt install git

Check Git is installed


git --version

Install GCC
sudo apt install build-essential

57
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev

Install Emacs:
sudo apt-get install emacs

Vim – should be installed by default

Nano – should be installed by default

58
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev

Install Emacs:
sudo apt-get install emacs

Pick One
Vim – should be installed by default

Nano – should be installed by default

59
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev

Install Emacs:
sudo apt-get install emacs

Vim – should be installed by default

Nano – should be installed by default


You will probably want copy/paste between WSL windows and regular
Windows windows – if so read/follow instructions in:
https://defragged.org/2020/10/29/how-to-copy-paste-in-windows-subsystem-for-linux-wsl/
60
The Programming Language C

61
62
This is a text editor
(Emacs)

63
This is a text editor
(Emacs)

It knows enough
about C to help you

64
65
Library reference (input/output)

66
The main program –
the thing that actually “runs”.

67
Function to print out a string

68
Program is done

69
70
More details later in lecture

71
72
This is the
command shell

73
This is the
command shell

It is also called the Command Line Interface (CLI)


Mac: /Applications/Utilities/terminal (or terminal.app)
Windows: c:/Windows/System32/cmd.exe
(by default Windows use different CLI commands)
74
ls lists files
“hello*” is a pattern

75
ls lists files
“hello*” is a pattern

by default, Windows uses dir instead of ls

76
“gcc” invokes a C
compiler

77
Compilers: gcc vs clang
The CS 5008 videos will show you can use either gcc or clang to “compile” your C
programs to run on the Khoury servers – and they suggest you choose one

gcc is the c compiler from http://gcc.gnu.org


• it is open source free software to compile C
• there is a version for almost all machines and operating systems

clang is a compiler front end from https://clang.llvm.org


• It supports the C, C++, Objective-C and Objective-C++ programming languages, as well as the
OpenMP, OpenCL, RenderScript, CUDA and HIP
• by default it also uses a newer C standard than does gcc

In almost all cases the compiler commands we will use are identical for gcc and clang –
though in the videos you will see some minor differences between the two

78
Compilers: gcc vs clang
The CS 5008 videos will show you can use either gcc or clang to “compile” your C
programs to run on the Khoury servers – and they suggest you choose one

gcc is the c compiler from http://gcc.gnu.org


• it is open source free software to compile C
• there are version for almost all machines and operating systems

clang is a compiler front end from https://clang.llvm.org


• It supports the C, C++, Objective-C and Objective-C++ programming languages, as well as the
OpenMP, OpenCL, RenderScript, CUDA and HIP
• by default it also uses a new C standard

In almost all cases the compiler commands we will use are identical for gcc and clang –
though in the videos you will seeI will
some useminor differences
“gcc” instead between the two
of “clang”

79
“gcc” invokes the C if no “-o” then executable
compiler would be “a.out”

80
you now have 2 files –
the new one is
executable

81
Run the
program with
”./hello”

82
Run the
program with
”./hello” The output ♬ Ta Da ♬

83
Run the
program with
”./hello” The output ♬ Ta Da ♬

If you are on Windows, your executable is “hello.exe” and


you would just type ”hello” to the CLI

84
What does the C compiler do?

diagram uses ”hello.c” instead of “helloworld.c”

85
What does the C compiler do?
your
source
program

86
What does the C compiler do?
C program
with a lot
more
details

87
What does the C compiler do?
language
the
computer
knows

88
What does the C compiler do?
format the
computer
knows
(0,1)

89
What does the C compiler do?
program
with all
library
functions

90
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s
List Contents of Object File
cat helloworld.o (unreadable)
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
91
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s cat is the Unix command to print
(catalog) the contents of a file
List Contents of Object File
cat helloworld.o (unreadable) By default, Windows uses type
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
92
93
Assembly Code

94
Assembly Code

call the printf


function

our string

95
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s
List Contents of Object File
cat helloworld.o (unreadable)
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
96
Object File

CS 5008 - Fall 2022 - Week 1 97


Object File
These numbers are in HEX
Base 16 “digits” are:
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f

a base 16 “digit” uses 4 bits: 00002

a ”byte” is 8 bits: 000000002


or 0x00 (base 16 in C)
0x00...0xFF = 010...25510

0xf = f16 = 1510


0xc7 = c716 = 19910

CS 5008 - Fall 2022 - Week 1 98


Object File

byte
addresses

CS 5008 - Fall 2022 - Week 1 99


Object File

machine
instructions

CS 5008 - Fall 2022 - Week 1 100


Object File

equivalent
machine
assembler
instructions
code

CS 5008 - Fall 2022 - Week 1 101


note that Intel machine

Object File instructions can have different


lengths!

equivalent
machine
assembler
instructions
code

CS 5008 - Fall 2022 - Week 1 102


What should you care about here?
Hexadecimal numbers will be key to read what is going on with the
computer

The concept of libraries and how they get linked into your main
program

Some of the detail of assemblers and assembly languages – what kind


of instructions are actually used to implement your C program, in case
you need to do some serious debugging

103
Quick introduction to hardware
Very, very, very quick!

104
105
Central Processing Unit (CPU) takes one instruction from
memory, decodes it, and executes it

PC = Program Counter
ALU = Arithmetic Logic Unit

A small number of data items are stored in ”registers”

CPU has a central clock


Clock speed is measured in Hertz (cycles/sec) – MHz or GHz

106
Busses connect CPU to memory and other
peripheral devices

107
Data (and programs) are stored on Disk and
in Main Memory – and data is actually
moved in registers to be operated upon.

108
from: https://images.app.goo.gl/aguFzFGb2Tv8qdFE9

109
Quick introduction to operating
systems

110
What is an Operating System?

When you boot up a machine, you see your operating system booting up

Mac

Windows

Linux

111
What is an Operating System?

When you boot up a machine, you see your operating system booting up

Mac

Windows

Linux – this is what the Khoury servers run!

112
Many Different Operating Systems (OSs)

Windows

Linux

BSD
113
What is an Operating System?
OS is software that sits between an application and
the hardware
OS is a resource manager and allocator
○ Decides between conflicting requests for
hardware
○ Attempts to be efficient and fair in sharing

OS is a control program
○ Controls execution of user programs
○ Prevents errors and improper use

114
What is an Operating System?

115
Most Common Operating System Families
POSIX
○ Anything Unix-ish
○ for example, Linux, BSDs, Mac OS, Android, iOS, QNX

Windows
○ Computers shipped by Microsoft

Many other operating systems may exist specific for a specific machine (like
the old mainframes) or for a domain (e.g., an operating system for a car or
handheld gaming device)

116
Some Unix Command Line Commands
ls
• lower case L/lower case S
• list files in the directory you are currently in
• what the Mac Finder/Windows File Explorer shows you automatically
cd
• connect to a directory
• what clicking on a folder in Finder/File Explorer does
pwd
• show me the current directory path (print working directory)
cat
• print out the contents of a file (concatenate and print files)
man
• show the online manual for a command: “man cd” shows the manual for the cd command

117
Some Command Line Commands (Unix)
cd ~
• navigate to home directory

cd .
• navigate to current directory

cd ..
• navigate to parent directory

118
C basics
For the history of C – see paper listed in your homework for this week

119
Returning to our helloworld program

• main()is a function with an integer return type


• It also is a special function denoting the start of the C program
• There is exactly one main() per program

120
Returning to our helloworld program

• Each statement ends with a semicolon ;


• curly braces {} denote a block of code – also called a scope
• Scopes delimit the visibility of variables
• They serve a similar purpose to indentation in Python
• Note: Indentation is not significant in C – pick a style and stick to it
121
Returning to our helloworld program

• printf() is a function that prints a string


• \n is a special character that prints out a “newline”
• printf() also formats numbers for printing

122
Variables
Variables represent a piece of memory - a “box” into which you can store and retrieve data

Every variable in C has a “type” – it denotes the kind of data stored and the size of that data

Variables must be declared before being used

You assign value to variable with =

For example:
int x=5;
says that you are declaring a variable x to be an integer (one word of memory = 4 bytes)
and that it has been assigned an initial value of “5”

123
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage

Our programming model


• A linear array of memory locations containing bytes (8 bits)
• File systems

124
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage

Our programming model


• A linear array of memory locations containing bytes (8 bits)
• File systems

125
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage

Our programming model


• A linear array of memory locations containing bytes (8 bits)
• File systems

126
Memory
In reality…
• Processor registers
• Processor caches
• Main memory When you declare a variable to
• Virtual memory be an “int” you are telling the
• “Hard disks” system to allocate 4 bytes of
• Network drives/Cloud storage space for it
Our programming model
• A linear array of memory locations containing bytes (8 bits)
• File systems

127
Memory
In reality…
• Processor registers
• Processor caches
• Main memory When you declare a variable to
• Virtual memory be an “int” you are telling the
• “Hard disks” system to allocate 4 bytes of
• Network drives/Cloud storage space for it
Our programming model
• A linear array of memory locations containing bytes (8 bits)
• File systems

128
A basic program with a variable

CS 5008 - Fall 2022 - Week 1 129


A basic program with a variable

we declare a new variable x


it is an integer
we give it an initial value of 5
we print it out

CS 5008 - Fall 2022 - Week 1 130


A basic program with a variable

“first x: %d\n” is a string


• the %d is a place holder for an integer value
• the \n is the special newline character

x is a variable
• x’s value is stuck into the %d placeholder

CS 5008 - Fall 2022 - Week 1 131


A basic program with a variable

Once x has been declared it becomes


visible and usable throughout the
rest of the scope in which it is defined

CS 5008 - Fall 2022 - Week 1 132


A basic program with a variable

CS 5008 - Fall 2022 - Week 1 133


A basic program with a variable
this is a HORRIBLE program
it is confusing as all heck
but such poorly written programs are used to show features

CS 5008 - Fall 2022 - Week 1 134


A basic program with a variable

we have declared a variable x


it is an integer
with an initial value of 5

135
A basic program with a variable

the curly braces define a new scope


we declare a new x in that scope (why?)
it obscures the original x variable outside the scope
it goes away when we leave the scope

136
A basic program with a variable

after the nested scope we again print original x


then we assign a new value to x
and print it out

137
A basic program with a variable

138
What do we learn from that example?
How to declare a variable
Variables must be declared before being used
How to initialize a variable
How to assign a new value to a variable
How to print a string with a variable
Introducing a new scope {} lets you declare new variables, which will
hide variables of the same name from an outer scope – AVOID
DUPLICATING NAMES LIKE THIS – IT WILL CONFUSE HUMANS!
Variables “go away” when their scope closes
139
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program

Functions also represent mathematical operations that transform inputs into


outputs – think of trig functions like sin(x) or tan(x)

Functions must be declared before the are used

For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
140
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program

Functions also represent mathematical operations that transform inputs into


outputs – think of trig functions like sin(x) or tan(x)

Functions must be declared before the are used

For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
141
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program

Functions also represent mathematical operations that transform inputs into


outputs – think of trig functions like sin(x) or tan(x)

Functions must be declared before the are used

For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
142
A basic program with a function

CS 5008 - Fall 2022 - Week 1 143


A basic program with a function

CS 5008 - Fall 2022 - Week 1 144


if and if/else statements
Most serious programs contain more than a linear sequence of
statements – they require decisions to be made based on values of
variables

The if statement test the truth of a logical expression – and if the


expression is true then the body of the statement is executed
if (a == b) {printf (“they were equal!\n”);}

The if/else statement does the same comparison, but if the


expression is false then the else-clause is executed
if (a == b) {printf(“equal!\n”);} else {printf(“not equal!\n”);}

145
comparisons:
== != < <= > >=

if and if/else statements logical operators (and or not):


&& || !

Most serious programs contain more than a linear sequence of


statements – they require decisions to be made based on values of
variables

The if statement test the truth of a logical expression – and if the


expression is true then the body of the statement is executed
if (a == b) {printf (“they were equal!\n”);}

The if/else statement does the same comparison, but if the


expression is false then the else-clause is executed
if (a == b) {printf(“equal!\n”);} else {printf(“not equal!\n”);}

146
comparisons:
== != < <= > >=

if and if/else statements logical operators (and or not):


assignment is = && || !
equality test is ==
Most serious programsdon’t
contain
mix themmore
than a linear sequence of
up!
statements – they require decisions to be made based on values of
variables

The if statement test the truth of a logical expression – and if the


expression is true then the body of the statement is executed
if (a == b) {printf (“they were equal!\n”);}

The if/else statement does the same comparison, but if the


expression is false then the else-clause is executed
if (a == b) {printf(“equal!\n”);} else {printf(“not equal!\n”);}

147
for loop
Most programs do many repeated actions – and there are many ways
to tell C to repeat an action over and over again

The for loop is ideal for counting up (or counting down) a number of
steps

For example
for (x=0; x<10; x=x+1) {printf(“%d\n”,x);}
would iterate variable x from 0 to 9 with a step of 1 – each iteration
printing out the value of x on a new line

148
A basic program with if and for loop

CS 5008 - Fall 2022 - Week 1 149


A basic program with if and for loop

this is always false


so “else” is executed

CS 5008 - Fall 2022 - Week 1 150


A basic program with if and for loop

CS 5008 - Fall 2022 - Week 1 151


A basic program with if and for-loop

C programmers would write


x = x + 1
as
x += 1
or
x++ CS 5008 - Fall 2022 - Week 1 152
While loop
The for loop lets you bound iterations to some counter, but
a while loop bounds the iterations to some logical expression

For example
while (a < b) { a = a+1; b = b-1;}
checks the logical expression a<b – if it is true, it executes the body,
and retests the expression...but if false it just exits the loop

Be careful – it is easy to write a while loop that never exits!


153
A basic program with a while-loop

CS 5008 - Fall 2022 - Week 1 154


A basic program with a while-loop

CS 5008 - Fall 2022 - Week 1 155


More Variables
You can query the size of
a variable or a type with
sizeof()

156
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage

Our programming model


• A linear array of memory locations containing bytes (8 bits)
• File systems

157
Back to Variables
You can query the size of
a variable or a type with
sizeof()

CS 5008 - Fall 2022 - Week 1 158


C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x

159
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x

An address in memory is also called a “pointer”


In other languages, it is also called a “reference”

160
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x

161
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Address of c
and
address of i

162
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Tell C what
kind of
pointer &c
and &i is

163
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Tell C what
kind of
pointer &c
Telling/Forcing C to use a particular
and &i is
type for a variable is called a “cast”

It is often necessary, but can also be dangerous


164
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Print
pointer
format

165
C variables
Each variable has a name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x

166
C pointers
A “pointer” is another C data type

Each pointer holds the address of an item of data in memory

There are pointers for each type: pointer to int, pointer to char, etc.
• most importantly, pointers know about the size of the data item they are pointing to

The “empty” value of a pointer is NULL


• initialize pointers and check for NULL
• “NULL” is to “pointers” as “0” is to “integers”

You must declare pointers before they are used


• int* y; // pointer to int
• char* z; // pointer to char

167
Understanding pointers is one of KEY concepts to get out of this course

C pointers
A “pointer” is another C data type

Each pointer holds the address of an item of data in memory

There are pointers for each type: pointer to int, pointer to char, etc.
• most importantly, pointers know about the size of the data item they are pointing to

The “empty” value of a pointer is NULL


• initialize pointers and check for NULL
• “NULL” is to “pointers” as “0” is to “integers”

You must declare pointers before they are used


• int* y; // pointer to int
• char* z; // pointer to char

168
C pointers

169
C pointers

address of memory

value stored in
memory

170
171
“int*” tells C that &x is a
pointer to an integer

172
“void*” tells C that &y is a
pointer to something

173
To be safe, check if y is
NULL before accessing it

174
175
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the variable p points to

Example:
int x = 500;
int* y = &x;
printf (“%d %d\n”, x, *y); // prints 500 500

176
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the variable p points to

Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to

177
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the
*yvariable p points to y

Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to

178
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value
*yof the
x variable p points to &x y

Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to

179
180
x: 5

181
x: 5

px:

182
x: 100

px:

183
184
Arrays
Our first C data structure

185
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0

186
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0
Arrays must be declared before being used, for example
int numbers[5];
represents 5 locations in memory, each of which holds an integer:
numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]

187
CS 5008 - Fall 2022 - Week 1 188
declare array of
5 integers

CS 5008 - Fall 2022 - Week 1 189


initialize the
contents of the
array

CS 5008 - Fall 2022 - Week 1 190


print out array
as decimal
numbers

CS 5008 - Fall 2022 - Week 1 191


print out array
as hex numbers

CS 5008 - Fall 2022 - Week 1 192


CS 5008 - Fall 2022 - Week 1 193
194
declare array of
10 integers

195
initialize the 10
elements of the
array

196
print array out
using a for loop

CS 5008 - Fall 2022 - Week 1 197


198
199
declare and
initialize in one
statement

200
CS 5008 - Fall 2022 - Week 1 201
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0

Strings are represented as array of char - ‘\0’ terminates the string


Char constants use single quotes: ‘a’, ‘Z’, ‘$’, etc.

202
C Arrays
Arrays are sequential locations in memory
Arrays are
Arrays aretyped and– homogenous
fixed size they cannot change during the program execution
Typing determines how much space is allocated for each element
So...we declare strings to hold the MAXIMUM size number of characters we
Arrays
mightare fixed in size when declared – no growing or shrinking
need
Access arrays by indexing – starting at 0
and C uses the ‘\0’ character to show where the real string actually ends

Strings are represented as array of char - ‘\0’ terminates the string


Char constants use single quotes: ‘a’, ‘Z’, ‘$’, etc.

203
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0

All the C routines that know


Strings arerepresented
about manipulating as strings
array
of char - ‘\0’ terminates the string
expect this ‘\0’ terminator
Char constants use single quotes: ‘a’, ‘Z’, ‘$’, etc.

204
CS 5008 - Fall 2022 - Week 1 205
declare string
with max size
10 characters

CS 5008 - Fall 2022 - Week 1 206


adding in 7
characters in
string

CS 5008 - Fall 2022 - Week 1 207


terminate string
with ‘\0’ in 8th
slot

CS 5008 - Fall 2022 - Week 1 208


the NUL character ‘\0’ is NOT
the same as the NULL pointer

CS 5008 - Fall 2022 - Week 1 209


CS 5008 - Fall 2022 - Week 1 210
Alternative syntax for initializing arrays

211
Alternative syntax for initializing arrays

212
Another alternative syntax

213
Another alternative syntax

214
Another alternative syntax

This is the “natural” way to


initialize a string – C inserts the
‘\0’ terminator automatically

215
Lets see address of characters in string

216
Lets see address of characters in string

217
Lets see address of characters in string

when you take the &


of a variable, you
need to tell C the new
type via a cast

CS 5008 - Fall 2022 - Week 1 218


Lets see address of characters in string

219
Lets see address of characters in string

sequential byte
addresses each
offset by 1

220
Summary
How to compile and run a C program
Quick introduction to hardware
Quick introduction to operating systems
C basics

221
Summary
How to compile and run a C program
Quick introduction to hardware
Quick introduction to operating systems
C basics

What to remember?
• The different parts of the C compiler/linker tool pipeline
• Hexadecimal notation/arithmetic
• Basic components of a computer (CPU, memory, registers, etc.)
• The C programming statements
• Pointers are addresses of variables in memory

222
OPTIONAL Homework videos
Mike Shah has a series of “master class” videos at
https://www.youtube.com/playlist?list=PLvv0ScY6vfd-GGT-aUH31X2yXgBSYXo6t

See Mike Shah’s video on using an OS emulator (Virtual Box) at


https://www.youtube.com/watch?v=ln0bxbDkzPY&t

223

You might also like