CSC161 2010F, Class 04: An Introduction to C Overview: * About the C Programming Language. * Some C Basics. * Q & A. * GNU/Linux Lab III, Continued. Admin: * Assignment 1 is due tonight. Are there any questions? * How do I get rid of the psychologists? It feels like grep should be helpful. So read the man page. * The next assignment should be available on Friday. * Convo tomorrow. Be there! * For Friday, read Wikipedia on Emacs: http://en.wikipedia.org/wiki/Emacs. * For Friday, read chapter 1 of Raymond. What is C and why are we learning it? * C is a programming language * Imperative programming language * Uh - Universally Helpful * Focuses on operations that change the state of the machine * Assignment (also I/O) * Primarily explicit rather than implicit sequencing of operations * Low-level * Reveals the underlying machine model * You get to take advantage of it * You have to understand it * C is a moderately old language * Developed in early 1970's * First computers were ... 1940's * First high-level languages (not machine code or assembly) ... about 1960 * Earlier languages: Fortran, LISP, Cobol, Algol * Newer languages: Java, Flash/Flex, ..., Ada, Scheme, Perl, * Small and coherent * ONE designer - Dennis Ritchie * Focus on building Unix * Why C at Grinnell? * Learn underlying machine * You need some imperative language * C can be beautiful * Lots of languages derive from C * C++ * C# * Java * JavaScript In 151, you learned that there are six basic language features you need to master in order to write programs that implement algorithms. * Some set of basic operations and types * Assign to a variable VAR = EXP * Input getchar() * Ouput printf(...) * Increment a numeric variable ++var var++ * Compare with <, ==, >, <=, >=, != * Variables- a way to name things * In C, these need explicit type * General form TYPE NAME; * Example int i; * Repetition - a way to repeat basic operations while (TEST) { EXP; } for (INIT; TEST; INCREMENT) { EXP; } * Conditionals - Make choices * Most common form if (TEST) CONSEQUENT else ALTERNATE * One-legged form if (TEST) CONSEQUENT * A way to sequence operations * Separate with semicolons * Do one after another after another * Functions/subroutines - encapsulate (and often name) some operations * Defining * Scheme (define func (lambda (params) body)) * C RETURN_VALUE func(TYPED_PARAMS) { BODY } * E.g., int silly(int x, int y) { x++; y--' return x+x*y; } * Calling * Scheme * (func arg1 arg2 ... argn) * C func(arg1, arg2, arg3) Some Tips and More * When using less, you can type /text to search for text, 'n' (no quotation marks) searches for the next occurence