Lecture Outline for Week 1 I. Class Introduction - Course info sheet - Prerequisites : CSC148, MAT133 II. CDF accounts - Accounts listed on class home page - Machine location: Engineering Annex rooms 107, 201, 203, 206 - Door code: see instructor - Printer limit of 300 sheets per term! - Be a good user - don't play games when system is busy - faq - lists useful info - describes how to connect from home - rules - CDF account rules III. Unix - see A Students Guide to CDF - man - "manual" - ls - "list directory" - rm - "remove file" - cp - "copy file" - mv - "move file" - logout - obvious - mail - "send and read your mail" - Newsreaders - rn, xrn, trn - you should read the following groups - ut.cdf.csc270h - ut.cdf.announce - ut.cdf.general - Editors - vi, emacs, pico, nedit - cat - "print file to screen" - example: "cat /etc/motd" - prints message-of-the-day - more - same as cat but lets you page through the output - the Unix file system - directories and subdirectories - "tree" structure - mkdir, rmdir - "make and remove directories" - cd - "change directory" - pwd - "path of the current working directory" - ~hsc/classes/270/examples - the directory with all class examples - chmod ugo+/-rwx - "change permissions on a file" - Example: chmod go-rwx mycode.c - removes read/write/execute permissions for all but the file owner - commands can have argument flags - specified by a "-" - Example: ls -l directory - piping - can pipe the output of a command to another command or to a file - !, Ex: "ls -l | more", "cat filename | more" - >, Ex: "ls -l > listing" - <, Ex: "myexecutable < datain > dataout" - >&, Ex: "gcc mycode.c >& errors" IV. Why learn C? - a large amount of historical code written in C - still the best systems programming language - Unix written in C - learn the structured/procedural programming style - help understand how a computer works (like assembly) V. C vs Java - C is more dangerous - not type checked - C has much of the same syntax - if, for, while, switch, primitive data types - no classes, no exceptions - difficult I/O - C has simple types - characters, integers, floating points - pointers, arrays, structures, unions SEE EXAMPLES hello_world.c, hello_world2.c, hello_world3.c, sums.c VI. C program structure 1) includes 2) defines 3) global variables - not recommended 4) function prototypes 5) int main(void) { } 6) helper functions VII. C procedure structure return-type function-name(parameter list) { variable declarations code } VIII. C compiler - gcc - the compiler - a.out - unless specified, this is the default executable which is output by the compiler - gcc -ansi -Wall -pedantic -g -o outfile infile.c - these are useful flags to gcc - gdb - a C debugger, must have compiled with the -g flag IX. C contol flow is the same as Java - if, while, do-while, switch-case, for, continue, break commands all work the same in C as in Java X. Most relational and logical operators are same in C as in Java - >, >=, <, <=, ==, != - ||, &&, ! XI. Basic C data types - similar to primitive Java types, but C types only state the size of the data, not its intended use - char = 1 byte - short = 2 bytes - int = 4 bytes - a C data type example int x = 260; char c = 150; c = (char)x; What is the value of c? - floating point numbers are the same as in Java - float = 4 byte IEEE floating point - double = 8 byte IEEE floating point - no boolean type in C - in C, 0 = false, not 0 = true - can use #defines for true/false #define true 1 #define false 0 - can cause problems for beginners - example: int bool = 5; if (bool) vs. if (bool == true) XII. arrays - a contiguous sequence of memory - declared, initialized, and indexed as in Java int a[10]; - indexed from 0 to 9 int b[10][100]; int c[5] = {1, 2, 3, 4, 5}; c[0] = 1, c[3] = 4, etc. XIII. strings - no separate datatype for strings - a string is just an array of characters with the terminating character '\0' - must leave space for terminating character!!! - strings have a special initialization char text[STRING_LENGTH]; char words[6] = "Hello"; char words[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; SEE EXAMPLE: capitalize.c #include - contains useful string functions like strlen, strcmp, strcpy, strcat SEE EXAMPLE: concat.c XIV. Magic numbers - a numerical constant buried in the code - poor style - use #defines instead, NO MAGIC NUMBERS! XV. Procedures - break problem up into small sizes - a function/procedure lets us encapsulate some operation so we can use it and ignore its implementation - we have seen some procedures: printf, scanf, strlen, strcpy - procedures should not manipulate global variables - use parameters - EXAMPLE: xchoosey.c - all C arguments are "call-by-value" - changing value of parameter does not change variable's value in calling procedure - example: int p(int a) { a = 5; } main() { int a = 1; p(a); printf("%d\n", a); } - C scoping rules - the "scope" of a variable is the part of code in which in can be accessed - the scope of a local variable is just the procedure in which it is defined - function parameters are treated the same as local variables XVI. Comments - must be bracketed by /* ... */ XVII. #include - tells the compiler to place the contents of the file into your code at that location - can be #include - or #include "my-library.h" XVIII. #define - #define ITEM value - compiler does a text replacement in the code - wherever "ITEM" occurs, it is replaced with "value"