Goals for this week:
-
Reminder about Unix resources and vimtutor.
-
Reminder of setting up directory structure for CS31 course work.
-
Practice compiling and running C programs: gcc and using make.
-
Reminder of how to set up our Swarthmore GitHub accounts.
-
Introduction to Lab 1 and C syntax.
-
Help getting started with set-up and Lab 1
1. ssh into a CS lab machine
We are going to start by ssh’ing into a cs lab machine. From a Mac, open a terminal and follow along. From Windows, you can connect using Putty (see Lab 0 for more details).
From a terminal window on MacOS (or on Linux), enter the following to ssh into a CS lab machine (replace yourusername with your user CS username):
ssh yourusername@lab.cs.swarthmore.edu
This will prompt you for your CS password (note that when you type in
your password nothing is printed), and log you into some CS lab
machine (hawk in this example). You will then see the shell prompt,
at which you can enter a command (pwd
in this example):
Password:
Welcome to: hawk
Ubuntu 20.04 focal
hawk % pwd
/home/yourusername
2. Unix resources and vim
For Lab 1 you are required to use the vim editor. You can remind yourself
about vim, and some of the Unix commands for using our system by reviewing
the Lab 0 page. It includes more information about Unix
and running vimtutor
. There are also links to
other resources listed in Section Section 7 at the bottom of this page.
3. Weekly Lab Directories
Each week you will copy over some example code that we will talk through and practice during lab. These examples are related to the assigned lab, so remember to refer back to them, and back to the Weekly Lab page, as helpful resources when working on lab assignments.
Start by creating a weeklylabs
subdirectory in your cs31
directory. In
this directory
you will create subdirectories for each week, into which you will copy files
that we will work on in lab. This week you will create a week01
subdirectory.
Your directory structure for cs31 will look something like this:
/ | ------------ / | \ bin home usr ... | ------------- / | \ ... you ... | ------------------ / | \ ... cs31 ... | ------------------- / \ labs weeklylabs / / \ ... week01 week02 ... / Files you will copy over
Let’s start by creating this directory structure and copying over some files (you may have some of this directory structure already created from Lab 0).
Follow along with me these steps
(NOTE: comments about the command are after #
):
cd # cd with no argument switches to your home directory (/home/you/)
mkdir cs31 # create a directory named 'cs31'
cd cs31 # cd into cs31 (change current working directory to cs31)
pwd # print the current directory name
mkdir labs # create a 'labs' directory
mkdir weeklylabs # create a `weeklylabs` directory
cd weeklylabs
pwd
mkdir week01 # create a 'week01' directory
cd week01
pwd
# cp <source> <destination>: dot refers to your current directory
cp ~newhall/public/cs31/week01/* ./ # <-- note the dot means the current directory
ls
Makefile README firstprog.c functions.c
There is a Using Unix guide on the CS Swarthmore helppages that contains some helpful information: https://www.cs.swarthmore.edu/help/basic_unix.html.
4. C Programming
4.1. Compiling and running C programs
C is a compiled language. Compilation is a process that translates
C program code in a C source file into a binary machine-code form of
the program that the system knows how to execute (the computer
doesn’t understand high-level languages like C or Python or
Java, instead it understands low-level machine code). If compilation succeeds
(there are no syntax errors in your code), the compiler creates an executable
file (the deafult name of which is a.out
) that you can run from the
comand line. For more info, refer to the
basics of
compiling and running C programs.
We’ll use the gnu compiler, gcc
, to translate C to an executable form:
gcc firstprog.c
Then enter the path name of executable file on the command line to run it:
./a.out
gcc supports command line options to include debug information in the executable file (-g) and to specify the name of the executable file (-o filename) rather than use the default "a.out". Let’s try it out on one of the files you copied over:
gcc -g -o firstprog firstprog.c
./firstprog
Along with the code you copied over, there’s a Makefile. The Makefile
contains rules for compiling executables from the .c source files. To
execute these rules, type make
command:
make # this will compile all files as specified by the all: rule
make
is a convenient way to compile without having to type in a
long gcc
command every time.
To clean up all the files that were compiled by make
, you can run
make clean
:
make clean # this removes all files generated by make (the can be rebuilt)
4.2. First C program: main, variables, printf
Let’s open firstprog.c
in vim
:
vim firstprog.c
and look for examples of:
-
C comments
-
How to import a C library (stdio.h)
-
The main function definition, function bodies ( { } ), and C statements (end in ;)
-
Defining constants
-
Declaring variables (note all are declared at the top of the main function)
-
printf function (similar to Python formatted print stmt)
-
Note that all the code is inside the body of a function!
Now let’s compile and run the program:
make
./firstprog
There are some suggestions for changing the code in TODO
comments. Let’s try adding in the first change. When we change the code
we need to save it in vim, and then recompile the program before running
it again:
vim firstprog.c # edit the file, save and quit when done (:wq in ESC mode)
make # recompile
./firstprog # run
Save and Recompile
If you have used Python you may be used to making changes in your editor, saving, and running it immediately. In C, there is one extra step between saving the file and running which is recompiling it. Every time you change your C program, you have to recompile it to build a new binary |
4.2.1. printf formatted output
printf
uses placeholders for specifying how a value should be printed
(how its series of bytes be interpreted). See firstprog.c
for examples.
Here is a brief summary:
### Specifying the type:
%d: int (ex. -234)
%f: float or double (ex. -4.34)
%g: float or double
%s: string (ex. "hello there")
4.3. Functions program
Let’s next open the functions.c
program and look at an example of
a C function.
vim functions.c
And let’s look at examples of:
-
Function Definition
-
Parameters and local variable declarations
-
Return type
-
All function bodies are between ( { } )
-
Function Call
-
Function Prototype
Then compile and run to see what this program does:
make p
./functions
There are some TODO
comments in functions.c
with some suggestions for
things to try out in this code. We might try one together, and we encourage
you to try others on your own too.
4.3.1. more about C functions
The syntax for functions in C is similar to that in Python, except that C function definitions must define the return type of the function and type and name of each parameter. Here are two examples:
/* sum: a function that computes the sum of two values
* x, y: two int parameters (the values to add)
* returns: an int value (the sum of its 2 parameter values)
*/
int sum(int x, int y) {
int z; // a local variable declaration
z = x + y; // an assignment statement
return z; // return the value of the expression z
}
/*
* a function that does not return a value has return type void.
*/
void blah( ) {
printf("this function is called just for its side effects\n");
}
An example of calling these two functions from main
looks like this:
int main() {
int p; // local variable declaration
p = sum(7, 12); // call to function that returns an int value
printf("%d\n", p);
blah(); // call to void function (doesn't return a value)
return 0;
}
5. One-time git / GitHub configuration
Before using Swarthmore’s GitHub Enterprise, you’ll need to complete some one time setup steps. and follow steps to clone your Lab1 repo using git: GitHub for lab checkout
6. Lab 1 Intro
Next, we’ll take a look at the first lab assignment. Note that all course assignments and lab practice (lab assignments, weekly labs, and homework assignments) will be posted to the Schedule part of the course webpage.
7. Handy Resources
-
CS Department Help page: https://www.cs.swarthmore.edu/help/basic_unix.html
-
Chapter 1 on C programming in course textbook