C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1
Outline Today: basic syntax, compilation Next time: pointers, I/O, libraries Goal: soundness, not completeness http://www.cs.umass.edu/~ransford/reuc/ 2
Why learn C? It s everywhere. Operating systems Networking Embedded systems Graphics Other languages (Python, Ruby, Perl,...) Space shuttles! Robots! 3
C vs. C++ C not object-oriented C C++ C easy to parse C easy to compile 80 60 40 20 0 Amount of Stuff 4
C vs. C++ C not object-oriented C C++ C easy to parse C easy to compile 80 60 40 why C is everywhere! 20 0 Amount of Stuff 4
Hello, World hello.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: printf("moo.\n"); 6: 7: return 0; 8: } 5
Compiling C My favorite compiler: GCC (free) gcc -o executable source e.g.: 6
Math library sin.c: 1: #include <stdio.h> 2: #include <math.h> 3: 4: int main (int argc, char** argv) { 5: float angle = M_PI / 2.0; /* in radians */ 6: float sine = sin(angle); 7: 8: printf("the sine of %f is %f\n", angle, sine); 9: } 7
Things you ll find in C /* comments of any length */ // single-line comments assignment = expressions; function(calls); control flow #compiler directives 8
Control flow 1 if (<condition>) { // do this if <condition> is true } else { // otherwise do this } 9
Control flow 2 while (<test>) { } // body goes here do { What s the difference? // body goes here } while (<test>); 10
Control flow 3 for (<initialization>; <test>; <update>) { } // body goes here <initialization>; while (<test>) { (is the same as) } // body goes here <update>; 11
Control flow 4 switch (<thing>) { case <value1>: <action1>; break; case <value2>: <action2>; break; default: <default_action>; } Control cascades downward. 12
Loops example loops.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: int i; 6: int j = 0; 7: 8: // what is the value of j after this loop? 9: for (i = 0; i < 10; i++) { 10: j = j + 10; 11: } 12: printf("j = %d\n", j); 13: 14: while (j > 20) { 15: j = j - i; 16: } 17: printf("j = %d\n", j); 18: 19: do { 20: j = j - 1; 21: } while (100!= 100); 22: printf("j = %d\n", j); 23: 24: } 13
Functions <keywords> <type> <name> (<params>) { // body goes here return <value>; } int absolute_value (int number) { /*... */ } static void say_hello () { printf( Hello\n ); } 14
Function example absval.c: 1: #include <stdio.h> 2: 3: unsigned int absval (int value) { 4: if (value < 0) 5: return -value; 6: return value; 7: } 8: 9: int main (int argc, char** argv) { 10: int a = 10, b = -20; 11: printf("a = %d; b = %d\n", a, b); 12: 13: printf("absval(a) = %d\n", absval(a)); 14: printf("absval(b) = %d\n", absval(b)); 15: } 15
{... } C is block-structured. That means: { everything s in a block. } if (1 == 1) { a(); } else { b(); } Can omit braces for single statements Same as: if (1 == 1) a() else b() 16
Types Very few built-in types. Integer & float types Casting: (type)value, e.g.: char c = 5; int d = (int)c; 17
Built-in types example types.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: printf("integer types:\n"); 6: printf("char: %d bytes\n", sizeof(char)); 7: printf("short: %d bytes\n", sizeof(short)); 8: printf("int: %d bytes\n", sizeof(int)); 9: printf("long: %d bytes\n", sizeof(long)); 10: printf("long long: %d bytes\n", sizeof(long long)); 11: 12: printf("-------------\n"); 13: 14: printf("floating-point types:\n"); 15: printf("float: %d bytes\n", sizeof(float)); 16: printf("double: %d bytes\n", sizeof(double)); 17: 18: return 0; 19: } 18
User-defined types So-called composite types struct foo { int x; float y; }; foo.x = 5; foo.y = 8.5; 19
A user-defined type struct.c: 1: #include <stdio.h> 2: struct foo { int x; int y; }; 3: int main (int argc, char** argv) { 4: struct foo ecks; 5: ecks.x = 10; 6: ecks.y = 10; 7: printf("x=%d; y=%d\n", ecks.x, ecks.y); 8: return 0; 9: } 20
Familiar operators a+b, a-b, a*b, a/b a%b (a modulo b, the remainder of a/b) ++i, i++, --j, j-- a==b, a!=b,!(a==b) Bitwise: & (AND), (OR), ~ (NOT), ^ (XOR) 21
Bitwise operators bitmath.c: what will this print? 1: #include <stdio.h> 2: #include "bitmath.h" 3: 4: int main (int argc, char** argv) { 5: unsigned int a, b, c, d, e; 6: 7: a = 1; 8: b = a << 3; // shift left 9: c = b; // complement 10: d = c & 0x0000ffff; // bitwise AND 11: e = d 0x0f000000; // bitwise OR 12: 13: printf("a = "); print_binary(a); 14: printf("b = "); print_binary(b); 15: printf("c = "); print_binary(c); 16: printf("d = "); print_binary(d); 17: printf("e = "); print_binary(e); 18: } 22
Compiler directives #define A_NUMBER 42 Compiler replaces instances of A_NUMBER with 42 as it reads the program. #include <foo.h> #include bar.h // from standard location // from current directory 23
Memory Here goes. Ugh. In C, you manage your own memory. Allocate with malloc(), from stdlib.h Free with free(), ditto Requires pointers (next class) 24
Memory example memory.c: 1: #include <stdio.h> 2: #include <stdlib.h> /* provides malloc() and free() */ 3: 4: int main (int argc, char** argv) { 5: int* an_integer_pointer; 6: int size = sizeof(int); 7: 8: an_integer_pointer = malloc(sizeof(int)); // allocate memory 9: 10: if (an_integer_pointer == NULL) { 11: fprintf(stderr, "Failed to malloc %d bytes!\n", size); 12: return 1; 13: } else { 14: printf("successfully allocated %d bytes.\n", size); 15: free(an_integer_pointer); // release the memory 16: return 0; 17: } 18: } 25
C at UMass CS ssh username@fenris.cs.umass.edu pico someprogram.c gcc -o someprogram someprogram.c./someprogram 26
For Next Time Pointers, I/O, libraries, some tricks Assignment (optional but helpful); download from the web page http://www.cs.umass.edu/~ransford/reuc/ 27