1. C Java Python date 1972 1995 2000 (2.0) procedure object-oriented both ----------------------------- compiled to machine compiled to byecode (javac Hello.java) scripting language, interpreted by software code (gcc hello.c), runs on bare runs by another piece (javah Hello) machine of software(JVM) ----------------------------- static type static type dynamic type --------------------------- manual memory automatic memory management management with garbage collection ------------------------------ C is designed as a systems language (i.e. a language for writing operating systems and other low-level infrastructual code). As a result, it tries to be "close to machine" and simple (the original C compiler can run with only 8KB of memory!) 2. Basic C program #include void main() { int x = 10; printf("hello world x=%d\n", x); } 3. C primitive types type size(64-bit) example -->(unsigned) char 1 char c = 5; /* similar to java's byte type, different than java's char type. More on this later. */ (unsigned) short 2 short s = 12; (unsigned) int 4 int i = 0; (unsigned) long 8 long l = 0; float 4 float f = 1.0; double 8 double d = 1.0; -->pointer 8 int *x = NULL; /* java has no such type. we will discuss this in the next class*/ -->No boolean type, a non-zero integer represents true, a zero integer represents false int i = 0; if (i) /* same as i!=0 */ printf("true"); else printf("false"); 4. Control flow Identical in semantics to those in Java. if () {...} else if () {...} else {...} while () {...} do {...} while () for loop break (cause innermost enclosing loop/switch to exit immediately) continue (cause the next iteration of the enclosing loop to begin) add "if i > 5 break;" to program and ask what output is switch statement A example program that prints out all prime numbers less than 100 (this is much less efficient than the one you'll implement in Lab1) int p, k; for (p = 2; p < 100; p++) { for (k =2; k < p; k++) { if ((p % k) == 0) { //equivalent to if (!(p%k)) break; } } if (k == p) { printf("%d", p); } } 5. C operators Again, identical to those in Java Arithmetic: +, -, *, /, %, ++, -- Relational: ==, !=, >, <, >=, <= Logical: &&, ||, ! BitWise: see next 6. Bitwise Operator: Shifting (what it is, why use it? a faster operation than multiplication) char x = 6; 0000 0110 char y = x << 1; 0000 1100 (equivalent to y = x * 2) y = x >> 1; 0000 0011 (equivalent to y = x/2) char x = -6; 1111 1010 char y = x >> 1; 1111 1101 (sign extension, fills with ones) unsigned char ux = 0xfa; unsigned char uy = ux >> 1; 0111 1101 (no sign extension, fills with zeros) Do not shift by more than the width of the type, undefined behavior in C. 7. Other bitwise operators & bitwise AND |0 1 0|0 0 1|0 1 Let b be any bit (0 or 1) b & 1 = b (keep b unchanged) b & 0 = 0 ("clears" b) | bitwise OR |0 1 0|0 1 1|1 1 Let b be any bit (0 or 1) b | 0 = b (keep b unchanged) b | 1 = 1 ("sets" b) ^ bitwise exclusive OR (XOR) |0 1 0|0 1 1|1 0 ~ one's complement (flip all bits) & is often used to mask off some bits | is used to turn bits on unsigned char permission = 0; /* last 4 bits used to encode a user's permission*/ |_|_|_|_|_|r|w|x| Write the following 3 functions int is_writable(unsigned char p) { return (p & 0x2); } unsigned char set_writable(unsigned char p) { //return p with the writable bit set, and other bits unchanged return (p | 0x2); } unsigned char clear_writable(unsigned char p) { //return p with the writable bit cleared, and other bits unchanged return (p & (~0x2)); }