CS 11 C track: lecture 1 n Preliminaries n Need a CMS cluster account n http://acctreq.cms.caltech.edu/cgi-bin/request.cgi n Need to know UNIX n IMSS tutorial linked from track home page n Track home page: n http://courses.cms.caltech.edu/courses/cs11/material/c/mike/index.htmlAssignments n 1st assignment is posted now n Due one week after class, midnight n Grading system: see "admin page" linked from track home pageOther administrative stuff n See admin web page: http://courses.cms.caltech.edu/cs11/material/c/mike/admin.html n Covers how to submit labs, collaboration policy, grading, etc.Main textbook n C: A Software Engineering Approach, 3rd ed. by Peter A. Darnell, Philip E. Margolis n Thorough, readableSupplemental textbook n Kernighan and Ritchie: The C Programming Language, 2nd. ed. n 1st edition NOT acceptable n "ANSI C" n Good for referenceC: pros and cons n What C is good at n low-level programming n speed and memory efficiency n portability (sorta) n Bad things about C n unsafe!!! n low level of abstractionGetting started (1) n The "hello, world!" program: #include <stdio.h> int main(void) { printf("hello, world!\n"); return 0; }Getting started (2) n Make this into a file called hello.c using a text editor n e.g. emacs, vi, nedit, pico n Compile into a program and run: % gcc hello.c -o hello % hello hello, world! % n Woo hoo!Source code to executable (1) n What you write is called "source code" n Two kinds of source code files: n regular code (files end in ".c") n header files (files end in ".h") n Compiler turns source code into "object code" n (files end in ".o") n Linker turns object code file(s) into executable (no special file suffix)Source code to executable (2) n The program gcc is both a compiler and a linker n When you do this: % gcc hello.c -o hello n Then gcc n compiles hello.c to hello.o n links hello.o with system libraries n outputs the binary executable program hello n removes hello.oSource code to executable (3) n You can do each step individually: % gcc -c hello.c (compile only) % gcc hello.o -o hello (link only) n In this case, hello.o is not removed n Sequence: n compiling: source code to object code n linking: object code to binary executableThe C language - overview n Programs are built up of functions n Functions n take in arguments n compute something n return a result n The main() function n is where program execution startsData types (1) n All data in C has to have a specified type n Examples: n int (integer) n char (character) n float or double (approximate real number) n others n Variables hold data of a particular type only n Variables must be declared before useData types (2) n Type declarations: int i; /* name = i type = int */ char c; /* name = c type = char */ double d; float some_float = 3.14; n Identifiers: i, c, d, some_float n Optional initialization (e.g. some_float) n Booleans à 0 or nonzero (usually 1)Data types (3) n Strings: arrays of type char char some_string[9] = "woo hoo!"; char same_string[] = "woo hoo!"; n Much more on strings, arrays later n Other types: structs, pointersOperators (1) n Numeric: + - * / % n Assignment: = int i = 10; /* initialization */ int j = 20; /* initialization */ i = 2 + i * j; /* assignment */ j = j % 2; /* assignment */Assignment operator n Assignment works this way: n 1) Evaluate the right-hand side (RHS) of the assignment operator n 2) Assign the resulting value to the left-hand side (LHS) of the assignment operatorOperators (2) n What does i = 2 + i * j; mean? a) i = (2 + i) * j; b) i = 2 + (i * j); n * has a higher precedence than + n Use () to force other interpretationOperators (3) n Other assignment operators: n +=, -=, *=, ... i += 2; /* i = i + 2; */ n increment and decrement: ++, -- i++; /* i = i + 1; */ ++i; /* same */Operators (4) n Test operators: n compare two values n < <= > >= n == for testing equality n != for testing inequality n read "!" as "not"Operators (5) n Logical operators: n arguments are ints used as booleans n i.e. usually 0 or 1 (false or true) n ! operator is unary logical "not" n && operator is binary logical "and" n || operator is binary logical "or"Operators (6) int bool1, bool2, bool3, bool4; bool1 = 0; /* false */ bool2 = !bool1; /* bool2 --> true */ bool3 = bool1 || bool2; /* value? */ bool4 = bool1 && bool2; /* value? */Operators (7) n "Unary minus" operator: int var1 = 10; int var2; var2 = -var1; n Like – with nothing to the left n Negates the valueExpressions and statements n i + 2 * j is an expression (has a value) n i = j * k; is a statement n ends in a semicolon n also is an expression (value is value of i) n i = j = k = 0; is allowed n Equivalent to i = (j = (k = 0)); n NOT ((i = j) = k) = 0;Comments /* This is a comment. */ /* * Comments can span * multiple lines. */ // This is NOT a comment!Functions (1) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; }Functions (2) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; } nameFunctions (3) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; } argument listFunctions (4) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; } return typeFunctions (5) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; } bodyFunctions (6) n Functions take arguments and return values: int f(int x) { int y = 10; return y * x; } return statementFunctions (7) n Calling the function we just defined: /* in another function... */ int res; int i = 10; res = f(10); res = f(5 + 5); res = f(i); res = f(i*5 + i/2); n All of these are valid function calls n
View Full Document