CS120 Day 1: C & Unix Basics ----------------------------- - review syllabus - course prereqs: - Java programming or equivalent - no knowledge of C or C++ expected - big picture concepts to focus on - really understanding memory usage - testing & debugging - code refactoring - OO design - polymorphism - quickie review of programming language types - C "features" - obscure code - illicit hacking - system level programming - really efficient memory usage - shoot yourself in the foot - different language versions evolved over time - different implementations (compilers) based on platform/hardware >>>>> FOCUS ON DIFFERENCES BETWEEN JAVA AND C <<<<<< - read posted C tutorial to start >>>> USE gpa.c as sample program (unfinished) <<<< PROGRAMMING PROCESS ==================== - store source code in file.c - create source code with editor (emacs, vi, etc.) - compile & run - gcc file.c -> creates a.out -> run with ./a.out - gcc -o file file.c -> creates executable file -> run with ./file - explain .o files and compilation steps for C - pre-processing: include files, definitions - compile (program = source code -> assembly -> machine = object code) - link w/library routines - load into memory - execute = run in CPU - preprocessor directives #include #define THING value - macro/constant definitions - comments: /* long comment form */, // short end-of-line comment DATA TYPES =============================== - primitive data types - not as strongly typed as Java - exact sizes of each are implementation defined! (platform dependent) - char, short, int (default), long, float, double, long double - unsigned qualifier can apply to any (signed is default) - const qualifier (final in Java) - variables are never initialized for you!!! - booleans - no data type in old C, use ints - bool type in c99 w/true, false - 0 is false, non-zero is true - use in all control structures - ASCII characters, escape sequences - - isalpha, isdigit, etc. - tolower, toupper - arrays - declare: type var[CONST_SIZE] - declare w/init: type var = {val1, val2, ...}; - indices start at 0 - elements passed by reference to method - strings - not primitive, not class, array of characters - special null character '\0' to terminate - "string literal" - no + operator concatenation - for library functions - strlen(s) returns #chars before \0 OPERATORS ==================================== - operators - arithmetic - relational & logic - precedence - short-circuit evaluation - bitwise logical operators - bitwise shift operators SIMPLE I/O =========================== - #include - output - void putchar(char); - puts("some string"); - printf("format string", args...); - formats: (Appendix B) %i %d %f %c %s %n %ld %lf %o octal %x hex - field widths %cols.decf - eg. %.2f, %.6f right justified by default use %- to left justify - and cols works for %d and %s also - input - int getchar() - EOF predefined constant for end of file WORKING IN UNIX ===================== - give out class accounts - login - passwd to change password - command-line environment - two environment options: - remote: ugrad network: ssh/putty into ugradx.cs.jhu.edu - local: virtualBox/Lubuntu - basic file commands: ls, mkdir, cd, cp, mv, rm - viewing files: cat, more, less - printing: lp filename (check what default printer is) - file hierarchy and shortcuts: ., .., ~ - use scp (secure copy) to copy from ugrad to local machine & vice versa > scp localfile joanne@ugrad3.cs.jhu.edu:remotepath > scp joanne@ugrad3.cs.jhu.edu:remotepath/filename localfile - downloading a file from the web: wget http://someURL - I/O redirection - use > to send output of any command to a text file instead of screen: ls -al >myfiles.txt ./a.out >output.txt - use < to get input for any command from a text file instead of keyboard: ./a.out