1 / 30

CS 240 Programming in C

CS 240 Programming in C. Lecturer: Ramin Dehghanpoor Office: S-3-134 Email: ramin.dehghanpoor001@umb.edu Course: https://www.cs.umb.edu/~ramin/cs240 I got help from and used Dr. Cheung’s and Dr. Liang’s slides. Course Objectives. This course will teach you C Programming

Télécharger la présentation

CS 240 Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.


Presentation Transcript

  1. CS 240Programming in C Lecturer: Ramin Dehghanpoor Office: S-3-134 Email: ramin.dehghanpoor001@umb.edu Course:https://www.cs.umb.edu/~ramin/cs240 I got help from and used Dr. Cheung’s and Dr. Liang’s slides

  2. 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/LINUX File System and File Access

  3. Motivation • C is the language of choice for systems programming and embedded systems and it is the mother of all languages. • According to the May 2018 Tiobe Programming Community Index (rate language popularity) • Mastery of the material in this course may enable you to get a high paying job!

  4. Introduction • Syllabus, Lecture Notes and Schedule • Web: http://www.cs.umb.edu/~ramin/cs240/ • Required Textbooks • The C Programming Language, 2nd Ed., Kernighan & Ritchie • LINUX for Programmers and Users, 1st Ed., Glass & Ables

  5. Introduction • HW assignments • Assignment write-ups will be on my website • But HW must be done on our UNIX/LINUX systems! • Must adhere to the C coding standard http://www.cs.umb.edu/~ramin/cs240/C Coding Guidelines.pdf • Homework is 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! • If I discover any cheating, I’ll follow school policy!

  6. Introduction to Grading • HW assignments (40%) • 7 homework (The highest 6 are counted) • Exams (60%) • 3 exams (The highest two are counted)

  7. Getting Started • UNIX Account • Apply for a CS240 account in person: see operators in UNIX/PC Lab (S-3-157) or remotely: see instructions posted in http://www.cs.umb.edu/sp/resources/other/faqs/#FAQ02 • 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

  8. Getting Started • Putty • 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 or FileZilla for secure ftp • SSH Communications • Information: http://www.ssh.com/products/ • Download from: http://www.cs.umb.edu/~bobw/CS110/SSHSecureShellClient-3.2.9.exe • Download and Execute Installer as directed

  9. 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

  10. 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 (or FileZilla) to transfer the locally stored source file to the UNIX/LINUX system • SSH Secure Shell to run UNIX/LINUX commands • You are on your own to try this on your own PC! • Don’t send problem reports to “operator” for help!

  11. Getting Started (Mac and Linux) • Mac and Linux operating systems come built-in with terminal programs which offer a command line. • Use ssh command. Ssh username@hostname • Example: ssh ramin@users.cs.umb.edu

  12. Communication • You must send me an email indicating your preferred email address for announcements and lecture schedules. I will communicate with you by that email. • Monitor the course website.

  13. 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

  14. 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 Basic UNIX Guide posted on the website: http://www.cs.umb.edu/~ghoffman/linux/unix_cs_students.html

  15. 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

  16. UNIX File System Directory ~$ pwd  print working directory /home/ramin ~$ ls  list what is in there cs240 ~$ cd cs240  go to subdirectory ~/cs240$ ls  list what is in there test.c cd cs240 cd .. pwd Sub-directory ls Sub-directory File rm filename File mkdir New Sub-directory

  17. Hidden Files • There are hidden files on your home directory • not normally displayed by ls • Names of these files begin with ‘.’, e.g. .login • You can see them if you use ls with –A option blade64(3)% ls -A .cshrc .plan cs240.f02 mbox private_stuff .login cs105 cs240.old playpen public_html .msgsrc cs240 cs241 playpen2 student • .cshrc is a script file executed when shell is started • .login file is a script file executed at time of login

  18. UNIX/LINUX File Permissions ramin@itserver6:~$ ls –la drwxr-x--- 2 ramin phd 4096 May 26 19:16 cs240 r =read w =write x =execute - =no access Change file permission using the chmod command: ramin@itserver6:~$ chmod o+rx cs240; ls -la drwxr-xr-x 2 ramin phd 4096 May 26 19:20 cs240 directory User group Others

  19. 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 two ways • Use a PC in S-3-157, run Putty/SSH and vi or nano • Use your home PC, run Putty/SSH and vi or nano • 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

  20. Handing in Assignments • Turn in assignments as hard copy of typescript file and also send me your hw files by email before deadline. % script (Start recording typescript file) Script started, file name is typescript % ls –la (list directory entries) % cat hello.c (display source file) % gcc –m32 hello.c –o hello (compile source file in 32-bit mode) % ./hello (run executable file from the current directory) % exit (stop recording) script done on Thu Aug 23 11:30:02 2012 % lpr typescript (print typescript file at the unix lab)

  21. vi or vim Editor (Glass, Pages 82-92) • “vi” is a UNIX visual editor (vim is the LINUX version) • 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”

  22. “vi” Modes At UNIX prompt, type “vi [filename] ” Type “:” Command Mode: Type commands or move cursor over text Last Line Command Mode: Type “w file” to write file Type “r file” to read file Type “Enter” Type “i” Type “I” Type “a” Type “A” Type “o” Type “O” Type “R” (Glass, p. 58) Type “Esc” Insert Mode: Insert text at the current cursor position Type “q” or “q!” and to exit vi (Return to Unix prompt)

  23. 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)

  24. “vi” Commands • Movement Commands (Glass, page 86) 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 87) [n]x delete n characters at cursor [n]dd delete n lines at current line • You can find more vi commands in http://www.cs.colostate.edu/helpdocs/vi.html • For vi to display line numbers by default, create an .exrc file in your home directory that has the following line: set nu • Then save the .exrc file and exit. Logout and log back in. Your new vi session should show line numbers.

  25. hello.c Program(K&R, Page 6) /* hello: first homework assignment name: your name date: xx/xx/xx */ #include <stdio.h> int main(void) { printf(“Hello World!\n”); return 0; } comment C preprocessor directive C function statements

  26. 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 (void) { printf (“Hello World!\n”); return 0; } /* terminated by this -> */ int main(void) and return is required.

  27. 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>”

  28. 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; }

  29. 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” (character constants in K&R page 193) • All C program statements end with a “ ; ”

  30. 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

More Related