1. C Java procedure object-oriented ----------------------------- compiled to machine compiled to byecode (javac Hello.java) code (gcc hello.c), runs on bare runs by another piece (javah Hello) machine of software(JVM) ----------------------------- manual memory automatic memory management management with garbage collection ------------------------------ 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. */ -->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 int i, j, remainder; for (i = 2; i < 10; i++) { j = 2; while (j < i) { remainder = i % j;//i modulo j if (!remainder) { break; } j++; } if (j == i) { printf("%d", i); } } 5. C operators Arithmetic: +, -, *, /, %, ++, -- int x = 1 int y = x + 1234567890 /* y is ?? */ int z = y + 4000000000 z = ?? int x = 10 int y = x / 3 /* y is ?? */ int z = x % 3 /* z is ?? */ Relational: ==, !=, >, <, >=, <= int x = 10; x == 10 (T/F) x != 5 (T/F) x > 10 (T/F) x < 10 (T/F) x >= 10 (T/F) x <= 10 (T/F) Logical: &&, ||, ! int x = 10; int y = 5; x > 10 && y <=5 (T/F) x > 10 || y <= 5 (T/F) !(x > 10) (T/F) 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 | bitwise OR |0 1 0|0 1 1|1 1 ^ 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| permission |= 0x1; //turn on executable bit permission |= 0x2; //turn on writable permission |= 0x4; //turn on writable permission &= ~0x1; //turn off executable permission &= ~0x2; //turn off executable permission &= ~0x4; //turn off executable if ((permission & 0x1)) { // is executable bit on? ... }