Download Printable (Powerpoint) - UMass Boston Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 240
Programming in C and UNIX
Lecturer: Bob Wilson
Office: S-3-071
Phone: 617-287-6475
Email: [email protected]
1
Course Objectives
• This course will teach you C Programming
• It also covers several other related topics:
–
–
–
–
–
UNIX Commands
Compiler and Debugger
MAKE and Makefiles
UNIX File System and File Access
UNIX Processes and Shells/Shell Scripts
2
Motivation
• C is the language of choice for systems
programming and embedded systems
• Number of Programmers by Language:
– COBOL
– Visual Basic
– C/C++
3 million
1.5 million
1.1 million
• Mastery of the material in this course may
enable you to get a high paying job!
3
Introduction
• Syllabus and Lecture Notes
– Web: http://www.cs.umb.edu/~bobw
• Required Textbooks
– The C Programming Language, 2nd Ed., Kernighan &
Ritchie
– UNIX for Programmers and Users, 2nd Ed., Glass &
Ables
4
Introduction
• HW assignments
– Assignment write-ups will be on my website
– But HW must be done on our UNIX systems!
• All Homework MUST BE an Individual Effort
– You can answer questions for each other as long as you
acknowledge any help that you receive from others
– BUT DON’T HAVE OR LET ANYONE ELSE DO YOUR
HOMEWORK FOR YOU!
– Don’t change the owner, group, or mode on any of your
homework directories or files!! (NO chown, chgrp, chmod!)
– If I discover any cheating, I’ll follow school policy!
5
Getting Started
• Fill out the CS240 Student Survey Form on the
web page as soon as possible
• UNIX Account
– Apply for CS240 ASAP!
– (Science 3rd Floor UNIX Lab)
• Use Sun Blades in UNIX Lab
• Access to UNIX systems from your home PC
– Secure internet access is required now!
– Can not use TELNET or FTP for remote access!
– Must use Secure Shell 2 for remote access
– Can use Putty or SSH Communications S/W packages
6
Getting Started (SSH 2)
• Putty (Officially Recommended by Systems Staff)
– http://www.puttyssh.org (Non Commercial Downloads)
– Download: SSHSecureShellClient-3.2.5.exe
• SSH Communications (Recommended by me)
–
–
–
–
http://www.ssh.com/support/downloads/secureshellwk
Download Non-Commercial Version (.exe for Windows)
Or http://ce.uml.edu/sshwinclient.exe
Download and Execute Installer executable
• Student Recommended
– http://www.i-tree.org/gpl/ixplorer.htm (Windows)
– http://rsug.itd.umich.edu/software/fugu/ (MAC OS X)
7
Getting Started (Editor)
• Editing source files is a requirement
• Learn to use a UNIX editor program
– vi: a simple visual editor described in Glass
– emacs: a more complex visual editor that can
also be used as a complete development shell
(also described in Glass)
– pico: a simple text editor based on the pine
email program ( I personally like this one!)
8
Getting Started (Email)
• Use the UNIX program pine for your email
• Pine is supported by system staff – UNIX
mail program is not.
• Type command “man pine” for pine user
documentation
• Type command “pine” to access email
9
Communication
• You must monitor course web site and your UNIX
email account for announcements!!
• To contact me:
– Office: S-3-071
– Office Hours:
• Posted on my website
• UNIX: finger bobw
– Email: [email protected]
– Phone/Voice Mail: (617) 287-6475 (UMB)
– or (508) 668-7427 (Home) only if really stuck!
10
Java versus C
• CS110 (Java) is a prerequisite for CS240!
• Java and C are both “block structured” languages
– Compound statements defined by braces { }
– Similar if-else, for/while loops, expressions, etc
• Java is Object-Oriented, C is only procedural
• Java and C syntaxes look deceptively similar
• However, many small annoying differences
– Close but different semantics/syntax can be confusing
– Learn C as a different language – not just like Java!
– C99 is a bit closer to Java, but we’re not using it here!
11
Java versus C
• Java Compiler / Interpreter
–
–
–
–
Java source is compiled to byte code file (.class)
Byte code file is then interpreted by JVM
JVM is written in native machine code
Interprets program slower than native machine code
• C Compiler / Linker
–
–
–
–
C source is compiled to object file(s)
Object file(s) is/are linked to create an executable file
Executable file runs as native machine code
Executes program faster than interpreting byte code file
12
C programming
• You will be learning to write, execute, and
debug C language programs in this course
• We will not be just modifying programs that
have already been written and debugged!!
• We will spend most of the time in class on
the C language, MAKE, and the debugger
• This is the primary material for tests/quizzes
• Use Kernighan and Ritchie (K&R) textbook!!
13
UNIX Operating System
• You will be using UNIX to edit, compile, debug,
and run your C programs.
• We will not spend as much time in class on UNIX.
• You must learn to use UNIX as you go
• You can be held accountable for UNIX on exams
• Use UNIX Guide and Glass textbook
• Also can refer to the Umass Boston Website:
– http://www.cs.umb.edu/helproot/cs.guide/csguide/csguide.html
14
Basic UNIX Commands
(Responsible for these next time)
cat
cd
cp
logout
lpr
ls
man xxx
more
mv
mkdir
pwd
rm
rmdir
CTRL-c
display a file on your terminal screen (see also “more”)
change directory
copy a file
logout from your account
print a hard copy
list files in a directory
manual page for command xxx
display a file on your terminal screen - one page at a time
move a file from one place to another
create a new subdirectory
print working directory (pathname of directory you’re in)
remove (delete) a file
remove (delete) a directory
“Control” key and “c” key together – stop current command
15
UNIX File System
Directory
bobw
cd cs240
~bobw/cs240
pwd
~bobw/cs240/hw0
ls
cd ..
Sub-directory
Sub-directory
~bobw/cs240/hw0/assignment
mkdir
New Sub-directory
File
rm
File
filename
16
Hello World!
• Your first homework project is to create and run a C
program – “Hello World!” (K&R, p5+)
• Create a source file “hello.c” in one of three ways
– Use Unix Systems in UNIX Lab with vi, pico, or emacs
– Use Putty/SSH on PC to access UNIX with vi, pico, or emacs
– Use Notepad on your own PC and transfer the file
• Use “gcc” to compile and create a file named “hello”
• Run “hello” to see the printout on screen
• Use a “typescript” file to turn in assignments
17
hello.c (K&R, Page 6)
/* hello.c: first homework assignment
name: your name
date: xx/xx/xx
*/
#include <stdio.h>
int main( ) /* we’ll ignore arguments to main for now */
{
printf(“Hello World!\n”);
return 0;
}
18
C Source - Comment Lines
• Comment text is ignored by the compiler
/* This is a multi-line comment.
The compiler ignores both lines. */
• Be sure to include the closing “*/”
/* This is a multi-line comment
int main ( )
{
printf (“Hello World!”);
return 0;
} /* terminated by this -> */
19
C Source - #include …
• Because this program uses the Standard I/O
Library, it needs to include <stdio.h>
• In C programming, a “.h file” defines
– Macros (e.g. Names for constants)
– Prototypes for functions (e.g. printf itself)
• “gcc won’t compile “hello.c” with “printf”
function without the “#include <stdio.h>”
20
C Source – Main Declaration
• “int main ( )” is the function where the UNIX
system starts execution of your program
• Your function’s code is within “braces” { }
• Braces are usually placed on their own lines
{
function statements are here;
}
21
C Source - printf
• The Standard I/O Library provides you with a
function named “printf ( )”
• “printf ( )” prints argument as text on screen
• Argument is the text between the parentheses
(“Hello World!\n”)
• “\n” is a C convention for “end of line”
(For open book tests, remember K&R page 193)
• All C program statements end with a “;”
22