Tutorial 1 (Sep 19): Introduction, C basics
Topics
- Introduction
- Assignment Submission
- Remote Access (SSH)
- UNIX Basics
- Basic input and output in C - printf(), scanf()
In-Class Exercises
- Hello World [posted in separate page]
- Input/output in C [posted in separate page]
Introduction
- Office hours: MW 2-4PM, ICT 524 PLEASE SEND ME AN EMAIL BEFOREHAND
- My desk is inside a lab, please knock and ask for me if the door is not open. There are two doors
- Also available by appointment, email me.
Assignment Submission
- Assignments will be submitted via D2L dropbox
- The dropbox closes automatically. Don't wait until the last minute, and risk not submitting it on time.
- I will try to get all assignments marked within a week, hopefully sooner.
- Extensions: I cannot give extensions, please contact the prof if you have a good reason. If you contact me, I will just refer you to the prof instead.
- Code errors: If your code does not execute at all, you risk receiving zero on your assignment. TEST YOUR CODE ON THE SCHOOL SERVER.
Remote Access
Unless you want to setup your own dev environment for the course, I highly suggest you use the school servers. For this course, we will use the arm.cpsc.ucalgary.ca server.
These instructions help you setup PuTTY on Windows, so you can SSH into the school servers. For OSX users, you can just use Terminal which is pre-installed (instructions below).
For Windows:
- Download and install PuTTY. I recommend the latest release version, just select the "putty.exe" link.
- Enter the Host Name, Port 22, SSH. Type a name under Saved Sessions (I just used arm.cpsc.ucalgary.ca) and then click Save. Click Open. Next time you can just double-click this profile from the list.
- If you get this message, you can check to see if it matches the key below. If it matches, you can click Yes. If it doesn't match.... probably hit Cancel as you might not be connecting to the proper server.
- You should now be connected. Server name should be csa1, csa2, or csa3.
For Macs:
- Open the Terminal app which is pre-installed.
- Type in ssh This email address is being protected from spambots. You need JavaScript enabled to view it.
- If you get a message about the server key, check that it matches the key: ssh-rsa 2048 82:dc:27:29:99:f1:e9:e7:67:d3:40:04:44:c8:a2:c1. If yes, enter yes
- Enter your password.
UNIX Basics
You can write code in emacs/vi/vim/nano which are text editors you can access from the command line. Then you compile the .c files (gcc), and run the compiled results (./programname). Below are some of the common commands we will use in tutorial. For a more complete list, check our the CPSC department's page.
Command | Explanation |
ls | list files by name only |
ls -l | list file with some additional details (there are other "flags" you can use instead of -l) |
mkdir "CPSC355" | make a new folder called "CPSC355" |
rm "fileName" | remove a file called BadCode |
rm -rf "folderName" | remove a folder, "r" for recursive (include all files/subfolders) and "f" to ignore prompts (be careful...) |
touch "A1.c" | make a blank file called A1.c |
vim "A1.c" | edit A1.c in vim editor |
cd "path to director" | change directory (eg. cd "CPSC355 Assignments") |
cd ./folderName | a dot (.) is shorthand for the current folder's path, so you can use ./ instead of typing the path every time cd folderName = cd ./folderName cd ./subFolder1/subFolder2 mkdir ./ |
cd .. | two dots (..) is shorthand for the parent folder Example *currently in /home/undergrads/studentname/CPSC355/* cd .. *changed directory to /home/undergrads/studentname/* |
A typical assignment may go like this:
Command | Explanation |
ls | check the contents of the folder I'm in |
mkdir A1 | make a new directory called "A1" |
cd A1 | change to the "A1" directory |
vim A1Code.c | use vim editor to create a new C file |
<INSERT> for Windows, <i> for Macs, <FN+ENTER> for BootCamp | enter INSERT/RECORDING mode so I can start typing my code |
...type your code... | |
esc | get out of INSERT/RECORDING mode |
:x | save the file and exit (:q! to quit without saving) |
gcc A1Code.c -o A1Code | compile the new code (if you don't use -o and specify a name, it will be named a.out by default) |
./A1Code | run the new code (make sure you use the ./ to specify that your code is in the current folder) |
Useful UNIX Tips
Auto-complete: Typing entire file/folder names is a pain. If you type the beginning of the file name, then press TAB, it will try to auto-complete the file/folder name. If multiple files/folders match what you entered, it will fill in what it can, and you can type a few more characters to pick between multiple matching files/folders. Try it out, it'll be easier to understand and you will never forget this.
Repeat previous commands: Just press up/down to scroll through previous commands you typed.
Spaces in file names: If your file or folder has a space in it such as "Assignment Code.c", typing gcc Assignment Code.c will not work, because it is looking for the first string (block of text) only. You need to use double quotation marks to mark the entire file name as a single string, just like when you are writing code: gcc "Assignment Code.c"
Stop current program/command: CTRL+C will work most of the time, if you are stuck in a program or typing a command and want to return to the command line.
Basic Input in C - printf(), scanf()
Reference: Textbook chapter 7, sections 1-4.