Lecture 4 ---------- Announce -------- - office hours today and tommorrow, in phelps maybe 1413 Today -------- - quick review of last class + UNIX devel cycle - do a quick program and explain the parts - start learning the C language - begin with variables Review/Didn't Do ----------------- - more useful UNIX ideas - job control - shell variables - redirecting stdin/stdout - pipes and wildcards - developing in the UNIX environment ** - UNIX idea of small components put together to do complex task - several shells, text editor, compiler - 1.) open text editor to create/edit code file (somthing.c) - 2.) enter C code into file - 3.) compile the code using gcc - preprocess - compile - assemble - link - gcc codefile.c -o mybinary - 4.) run the program ./mybinary - 5.) goto 2 - every time code changes you must recompile for change to be reflected in binary - compiling phase checks for correct C syntax, generates much warnings/errors C Example ------------------------------ -------------------------------------- #include main() { /* Simple program to print hello world */ printf("Hello World\n"); } --------------------------------------- program disection ------------------ - #include - C language itself does not define any functions - standard libraries have been written so that programmers donot have to start from scratch - function definitions allow use of functions are in 'header' files, .h - #include is a preprocessor step which essentially finds the given file and puts it in place of #include stdio.h - main() { - defines a function called 'main' which takes no arguments '()' - statments are contained within curly braces {} - by default, OS will first call the main function of a program - /* comment */ - anything between /* and */ is a comment - no effect on program itself, thrown out during compilation - printf("Hello World\n"); - calling a function, defined in stdio.h, called printf. - passing the printf one parameter within the () - parameter is a 'string' delimited by "s - Hello World\n contains 12 characters, 10 letters, a space, and a 'newline' which tells the cursor to go to the beginning of the next line - semlicolon must be placed after all program statments - } - ends definition of the main function C Variables ------------ - a variable is defined as a placeholder for data that can change during the course of the program execution - when a variable is declared in the C program, when the process runs it reserves some memory for that variable ** picture - contiguous chunk, size determined by variable type, name in the code is a reference to the address of memory we're dealing with - most languages hide all this, in C we must understand - variable names have some restrictions - names can be made of letters and digits (_ is a letter) - must start with letter (int 1var = 10; will not work) - variable names cannot be reserved words like if, while, for, or any other name that is already used by the language to define some functionality - case sensitive! foo and Foo are two different variables - also some accepted practices: lower case for var names, upper for constants - variables in C must be declared at beginning of statement block - before you use a variable, it must have been declared somehwere previous - initial values can be specified upon declaration ex. int a; int a = 10; float b = 10.1212; - reason variables must be declared is that C is a strongly typed language - variables in C are 'typed'. type defines ex. types: int, char, float, double - what kind of data can be stored in the variable - int: pos and neg integers (-2, -1, 0, 1, 2, 3, 4...) - char: single ASCII characters (a, b, c, -, !, @, #...) - char actually stores an integer - man ascii - float: pos and neg single precision reals: (-1.001, -0.99, 0, 0.0001, 1.2, 3.4 ...) - double: pos and neg double precision reals: (1.000000000000001) - types have modifiers - int: short, long - double: long - all: unsigned, signed - what operations can be performed on the variable - most built in operators work for all variables, but some do not make sense - how much memory is reserved for the contents of the variable - this is importants and often ignored - each type has associated size - ex. char 1byte, int 4bytes, float 4 bytes, double 8 bytes - size of container is machine dependent, with lower limit defined by the standard - ex: pretend int was 1 byte 00000000 - what is the range that such an int could take? - turns out you need one bit for signage - 0 - 255 for unsigned, -128 - 127 for signed - use builtin sizeof() which takes type and returns size of type - type conversion - what happens if you have an expression with two separate types? - C specifies a promotion scheme, where smaller variables are first promoted to larger types before operation - z = d + i; - i is first promoted to a double, then the addition happens, then z is promoted, then assignment happens ??- tricky when unsigned types get thrown into the mix - programmer can impose type conversion through 'casting' - z = (int)d + i; - when fp variable is demoted with casting, after decimal point is truncated - this is dangerous! if d is outside range that can be stored in i, will be weird number - operators and expressions - operators special characters that indicate some operation to be performed on variables - expressions are made up of variables and operations - standard binary math operators: - +, -, *, /, %, = - unary operators - ++, -- - ++a and a++ - order imposed on expression evaluation - *, /, % have higher precedence than +, - - z = a + b * c; - use parens to impose your own order - z = (a + b) * c - conditional expressions - result in 1 or 0, true or false - &&, ||, !, != , ==, <, >, <=, >= - conditionals can be chained - order of ops important!!! - ex: 1 || 0 == 0 - trying to show 1 || 0 is true, which does not == 0 - should be false, but is true since == is evaled first! - evaluation stops when truth determined - ex: (1 && 1) || (0 || 1) - important because expressions can contain functions or other ways to modify a variable! ex: a = 0; b = 1; z = (a || b) || (++a && b); - what is the value of 'a' after this expression! - really flase and true are 'non zero' and 'zero'. - ex: z = (2 || 0) - Inputting and Outputting variables - predefined function called 'printf' - definition of printf - printf(, var1, var2, var3, ...) - is the format string used by printf - can contain characters, control characters, and conversion specifications - normal characters are printed as inputted - special charaters must be 'escaped' by using the \ character - \" - \\ - control characters send unprintable characters to the screen, - any character that does not really have a keyboard equiv - \n, \b, \a - conversion specifications is fancy for 'variable content placeholders' - ex: int a = 10; int b = 20; printf("The contents of variable a = %d\n", a); printf("%d %d %d\n", a, b, a); - program prints: > The contents of variable a = 10 > 10 20 10 > - %d is a conversion specification - %d, %c, %f, man 3 printf for more - normally, use conversion spec appropriate to type given - can 'convert' by using different specifiers - ex: char c = 'a'; printf("Integer value for variable c = %d\n", c); - program prints: > Integer value for variable c = 97 > - Reading values from the keyboard using scanf - scanf defn: scanf(, &var1, &var2, &var3, ...); - format string same as printf, but means different - scanf expects input in form specified by , and will replace variable contents with input in any place where conversion specifier is located. - ex: int a; printf("Enter an integer: "); scanf("%d", &a); printf("You entered %d\n", a); - scanf gotcha, reading charcters - scanf("%c", &c); - scanf("%c", &e); - newline falls through! read a temp var inbetween