Lecture01 1

Document Sample
Lecture01 1 Powered By Docstoc
					         CS 240
    Programming in C
      Lecturer: Dr. Ronald Cheung
             Office: S-3-073
         Phone: 617-287-6483
      Email: cheungr@cs.umb.edu
Course: www.cs.umb.edu/~cheungr/cs240
            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
• C is the language of choice for systems
  programming and embedded systems
• According to the July 2012 Tiobe Programming
  Community Index (rate language popularity)
            Position     Language
             1            C
             2            java
             3            Objective C
             4            C++
• Mastery of the material in this course may enable
  you to get a high paying job!
           Peer Mentor Help
• Secured funding for a peer mentor to help
  CS240 students
    for fall’12 - Kevin Amaral (kevin.m.amaral@gmail.com)

• Meet 2 times a week Mon/Wed 2:00-3:45pm
  in the Weblab (S-3-028)
• Student participation is voluntary
• Peer mentor will answer questions, do
  additional programming exercises to illustrate
  concepts, but ‘ll not do homework for you.
• Fill out the Student Information Sheet

• Syllabus, Lecture Notes and Schedule
   – Web: http://www.cs.umb.edu/~cheungr/cs240/

• Required Textbooks
   – The C Programming Language, 2nd Ed., Kernighan & Ritchie
   – UNIX for Programmers and Users, 3rd Ed., Glass & Ables

• Reference
   – C Programming for Scientists and Engineers with Applications,
     1st Edition, Rama Reddy and Carol Ziegler, Jones and Bartlett
• HW assignments
   – Assignment write-ups will be on my website
   – But HW must be done on our UNIX systems!
   – Must adhere to the C coding standards
   http://www.cs.umb.edu/~cheungr/cs240/C Coding Standards.htm

• Homework is an Individual Effort
   – You can answer questions for each other as long as you
     acknowledge any help that you receive from others
   – If I discover any cheating, I’ll follow school policy!
                Getting Started
• UNIX Account
  – Apply for a CS240 account ASAP!
     in person: see operators in UNIX/PC Lab (S-3-157)
     remotely: see instructions posted in

• Access to UNIX systems using PCs in the
  UNIX/PC Lab or

• 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
                    Getting Started
• Putty (Officially Recommended by Systems Staff)
  –   http://www.chiark.greenend.org.uk/~sgtatham/putty/
  –   Download, UnZIP and Execute Installer as directed
  –   Use Putty configured for SSH2 instead of TELNET
  –   Use PSCP or PSFTP for secure ftp

• SSH Communications (recommended by me)
  – Information: http://www.ssh.com/products/
  – Download from:


  – Download and Execute Installer as directed
              Getting Started
• You may want to use your own PC as a “front
  end” to our UNIX systems

• This is the way modern SW development
  environments in industry are set up now

• Possible using the SSH Communications secure
  access software package

• Have four windows open on your PC
                 Getting Started
• Four windows:
   – Window to local file folder for your source files
   – Notepad to edit/save a text source file locally
   – SSH Secure File Transfer to transfer the locally stored
     source file to the UNIX system
   – SSH Secure Shell to run UNIX commands

• You are on your own to try this on your own PC!

• Don’t send problem reports to “operator” for help!
• You must monitor the course web site and
  your UNIX email account for
  announcements and lecture schedules!!

• If you are not reading your UNIX mail
  regularly, change the .forward file in your
  UNIX home directory to forward mail to
  another email account.
             C Programming
• You will learn to write, execute, and debug C
  language programs in this course

• We will spend most of the time in class on the C
  language, MAKE, and the debugger

• This is the primary material for exams

• Use Kernighan and Ritchie (K&R) textbook!!
   UNIX/LINUX 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 will be held accountable for UNIX on exams
• Use the Glass and Ables textbook
• Also refer to the UNIX Guide posted on the website:
    Basic UNIX Commands
cat      display a file on your terminal screen (see also “more”)
cd       change directory
cp       copy a file
logout   logout from your account
lpr      print a hard copy
ln       creates a new link to a file
ls       list files in a directory
more     display a file on your terminal screen - one page at a time
mv       move a file from one place to another
mkdir    create a new subdirectory
pwd      print working directory (pathname of directory you’re in)
rm       remove (delete) a file
rmdir    remove (delete) a directory
CTRL-c   “Control” key and “c” key together – stop current command
             UNIX File System
~cheungr                              Directory
                        cd cs240                       cd ..
~cheungr/cs240                 pwd   Sub-directory

~cheungr/cs240/hw0        ls         Sub-directory             File

                                                  rm     File
             mkdir   New Sub-directory            filename
     Home Work Assignment #1: 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 a PC in S-3-157, run Putty/SSH and vi or emacs
   – Use your home PC, run Putty/SSH and vi 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

• Run “script” to create a “typescript” file and run “exit” to end the
  script file

• Turn in the “typescript” file for grading
        Handing in Assignments
• Turn in assignments as hard copy of typescript file
   % script                     (Start recording typescript file)
   Script started, file is typescript
   % ls –l                      (list directory entries)
   % cat hello.c                (display source file)
   % gcc hello.c –o hello       (compile source file)
   % hello                      (run executable file)
   % exit
   script done on Thu Aug 22 14:30:02 2002
   % lpr typescript             (print typescript file at the lab)
      vi Editor (Glass, Pages 57-69)
•   “vi” is a UNIX visual editor
•   Keyboard oriented – no use of a mouse!
•   At UNIX prompt, type “vi hello.c”
•   “vi” opens an existing file / creates a new one
•   “vi” has three modes (See next slide)

    – “Command mode”
    – “Insert mode”
    – “Last line command mode”
                     “vi” Modes
       At UNIX prompt,
     type “vi [filename]     ”

                                    Type “:”

               Command Mode:                  Last Line Command
Type “i”       Type commands or      Type            Mode:
Type “I”      move cursor over text “Enter” Type “w file” to write file
                                             Type “r file” to read file
Type “a”
Type “A”                           Type “Esc”
Type “o”
                   Insert Mode:
Type “O”          Insert text at the           Type “q” or “q!” and
Type “R”       current cursor position               to exit vi
(Glass, p. 58)                                (Return to Unix prompt)
    vi’s Text Entry Mode Commands

Key                    Action
i       Text is inserted in front of the cursor
I       Text is inserted at the beginning of the current line
a       Text is added after the cursor
A       Text is added to the end of the current line
o       Text is added after the current line
O       Text is inserted before the current line
R       Text is replaced (overwritten)
               “vi” Commands
• Movement Commands (Glass, page 61)
     Up one line            “cursor up” or “k” key
     Down one line          “cursor down” or “j” key
     Right 1 char           “cursor right” or “l” key
     Left 1 char            “cursor left” or “h” key
• Edit commands (Glass, page 62)
     [n]x                   delete n characters at cursor
     [n]dd                  delete n lines at current line
• You can find more vi commands in
• If you want to set up your environment such that vi
  always displays line numbers, modify your .login
  file to have the line:
      setenv EXINIT ‘set nu’
    hello.c Program(K&R, Page 6)

                 /* hello: first homework assignment
                    name: your name
                    date: xx/xx/xx
C preprocessor
                 #include <stdio.h>
C function       int main(void)
                    printf(“Hello World!\n”);
                    return 0;
      C Program - 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 “*/”.

• For example:
      /* This is a multi-line comment
      int main ()      In C89 version, main() is the same as
     {                 int main(void) and return is required.

            printf (“Hello World!\n”);
            return 0;
      } /* terminated by this -> */
         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 the “printf”
  function without the “#include <stdio.h>”
   C Source – Main Declaration
• “int main (void )” is where the UNIX system starts
  execution of your program

• Your program is between the “braces” { }

• Braces are usually placed on their own lines
            program statements are here;
             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

• “\n” is a C convention for “end of line”
   (character constants in K&R page 193)

• All C program statements end with a “ ; ”
             Character Constants
•   New line         \n   backlash                      \\
•   Horizontal tab   \t   question mark                 \?
•   Vertical tab     \v   single quote                  \’
•   Backspace        \b   double quote                  \”
•   Carriage ret     \r   octal number                  \ooo
•   Form feed        \f   hex number                    \xhh
•   Audible alert    \a   For example:
                               \n = \12 = \x0A = newline character

Shared By: