CSC 357 Lecture Notes Week 1
Introduction to the Course Introduction to C and UNIX



  1. Relevant reading.
    1. K&R chapters 1 through 4; chapter 7, excluding 7.3, 7.4
    2. Selected parts of Stevens and selected man pages, as cited in the lab and programming assignment writeups.

  2. Go over syllabus.

  3. Go over Lab 1.

  4. Go over Programming Assignment 1, including test plan.

  5. Go over "UNIX Basics" handout.

  6. Go over "Coding Conventions" handout.

  7. A brief history of C.
    1. The first edition of The C Programming Language was published in 1978 by Brian Kernighan and Dennis Ritchie; this work has come to be known as "K&R".
    2. In 1983, work began to standardize the language, under the auspices of American National Standards Institute (ANSI).
    3. The second edition of K&R was published in 1988, defining the ANSI standard version of the language; interesting facts about K&R:
      • The book is in it 39th printing.
      • It was typeset using troff, the same wonderfully arcane document formating system used for these notes, and all other CSC 357 material this quarter.
    4. Since its inception, C has been used to write gazillions of lines of code, at the systems level, as well as for higher-level application programs.

  8. Brief history and variants of UNIX.
    1. Bell Labs and AT&T (originated in late 1960s).
    2. BSD, the Berkeley Software Distribution (early 1980s to present).
    3. SUN OS and Solaris, from SUN Microsystems (mid 1980s to present).
    4. Various other workstation vendors, including HP and IBM (1980s to present).
    5. CMU's Mach, and other university efforts (mid 1980s).
    6. Apple, A/UX and Mac OS X (1980s and present).
    7. Linux (early 1990s to present).
    8. CYGWIN, a UNIX emulator for Windows (mid 1990s to present).
    9. POSIX interface (late 1980s to present).

  9. Key features (and non-features) of the C language.
    1. It's a low-level language, meaning it has features that put it closer to the computer hardware and underlying operating system than other higher- level languages, like Java, e.g.
    2. It's a simple language, compared to many others.
    3. It has no classes or other OO constructs, and no garbage collection.

  10. Key similarities and differences between Java and C.
    1. Most of you in CSC 357 have used Java in your programming classes.
    2. In terms of fundamental programming constructs, C and Java are very similar.
      1. if-else and switch conditionals are essentially the same.
      2. for and while loops are very much the same.
      3. function (aka method) declarations and invocations are fundamentally the same
    3. Except for certain particulars of primitive data types, the language features covered in chapters 1-4 of K&R are very similar indeed to Java.
    4. Functions in C, compared to methods in Java.
      1. The terms "function" and "method" are largely synonymous.
      2. Since C has no classes, a function is defined as a top-level program construct, and invoked directly by its name, without class-name qualification.
      3. Formally, the way C and Java pass arguments are essentially the same; both use strictly call-by-value semantics.
      4. Call-by-reference parameter passing in C is not a matter of function-call semantics, but of explicit pointer semantics, which we'll discuss next week.
    5. The key differences between Java and C are these:
      1. C has no classes, no language-provided exceptions, and no language-coupled GUI library.
      2. C uses explicit pointers, compared to the more implicit references of Java.
      3. C has features that provide low-level memory access that are missing from Java.

  11. An introductory C example, from Chapter 1 of K&R.
    #include "stdio.h"      /* Include standard I/O library */
    
    main() {        /* Define main function */
        printf("hello, world\n");  /* Print to stdout */
    }
    
    1. Compile this program using
      gcc hello.c
      
    2. Run it using
      a.out
      

  12. A side-by-side comparison of Java and C versions of the "Hello world" program.
    import java.io.*;   // not necessary in this case, but parallels #include "stdio.h"
    
    public class hello {            // not present in C
    
        public static void main(String[] args) {        // simpler in C
            System.out.println("hello, world");          // simpler in C
        }
    
    }
    
    1. Compile with
      javac hello.java
      
    2. Run with
      java hello
      

  13. Standard input and output in C (K&R Chapters 1 and 7).
    1. The streams stdin and stdout are the basic way to get data in and out of a C program.
    2. Formated output is with printf.
      1. There are numerous examples throughout K&R.
      2. Section 1.2 has a good introduction.
      3. Section 7.4 has details
    3. Character input/output is with getchar and putchar.
      1. We'll use these regularly in 357.
      2. Section 1.5 has details and examples.
    4. Formated input with scanf.
      1. We won't use scanf a lot 357.
      2. There are various examples in K&R.
      3. Section 7.4 has details.

  14. Introduction to strings as character arrays in C (K&R Section 1.9).
    1. In C, strings are represented not as a separate data type, but as arrays of characters.
    2. Next week we'll cover full details of C arrays; for starters, here's what you need to know about char arrays:
      1. You can declare an empty string variable to hold a fixed number of characters using a normal-looking array declaration, such as
            char string_var[100];  /* a string variable that can hold up to 100 chars */
        
      2. You can assign values to such a string on a character-by-character basis; e.g., the following code sets string_var to "abc".
            string_var[0] = 'a';
            string_var[1] = 'b';
            string_var[2] = 'c';
            string_var[3] = '\0';      /* Null termination */
        
        1. Note that to be properly formated, a null character must follow the last valid data character in a string; i.e., the string must be null-terminated.
        2. All of the C library functions that you'll use in Lab 1 and Program 1 deal with null-terminated strings.
      3. C functions that take strings as arguments almost always declare the strings using a character array of an unspecified length, such as the following:
        void f(char string_arg[]) { /*...*/ }
        
        1. The empty brackets mean the string_arg parameter can accept string argument values of any size.
        2. An equivalent, and frequently-used notation for string parameters is "char *", as in
          `void f(char *string_arg) { /*...*/ }
          
          which is completely equivalent to the previous definition of function f.
      4. You can use double-quoted string constants in a normal-looking way, as in the following function call:
        f("xyz");
        
      5. A short-hand way to initialize a string-constant variable is as follows:
            char* string_const = "xyz";
        
        1. You must be sure not to try to assign a value to such a string constant, since it's value is fixed.
        2. We'll discuss exactly why next week.
      6. Section 1.9 of K&R has a useful introductory example for dealing with string variables.
      7. We'll cover full details of the * and [] notations next week.

  15. C types, operators, and expressions (K&R Chapter 2).
    1. Primitive data types (K&R Section 2.2).

      Data type Description
      char a single byte, capable of holding one character in the local character set
      int an integer, typically reflecting the natural size of integers on the host machine
      float single-precision floating point
      double double-precision floating point

      with qualifiers short, long, signed, and unsigned.
    2. Constant declarations (K&R Section 2.3).
      1. Use '#define' to declare constant values.
      2. E.g.,
        #define LINE_LENGTH 72
        
      3. There is also the enum declaration, as introduced on pg. 39 of K&R.
    3. Arithmetic and relational operators (K&R Sections 2.5 and 2.6).
      1. These are very much the same as Java.
      2. See the cited K&R sections for details.
    4. Type conversions (K&R Section 2.7).
      1. In general, C does fewer automatic data conversions than Java.
      2. Many things convert to int by default, with some interesting results.
      3. We'll discuss details in upcoming lectures.

  16. C Control flow (K&R Chapter 3).
    1. Very much the same as Java.
    2. Notably missing from Java is goto, which you should not use in C.

  17. Functions and program structure (K&R Chapter 4).
    1. As noted above, a function declaration and invocation in C is much like methods in Java.
    2. A C program is a collection of .c files, exactly one of which contains a main function.
    3. C programs also typically have .h files (header files), that define function signatures and type definitions
      1. A pair of .c and .h files, with the same root file name, are they way to define the rough equivalent of a class in Java.
      2. We will discuss this form of program structure further in upcoming lectures and examples.

  18. File access (K&R Chapter 7).
    1. Files can be opened with fopen and fclose.
    2. Functions getc and putc perform character input/output on files.
    3. Functions fprintf and fscanf do formatted input/output on files.
    4. Functions fgets and fputs do whole-line input/output on files.
    5. Sections 7.5 and 7.7 have details.

  19. Key differences between UNIX and Windows.
    1. In general, UNIX users spend a lot more time in terminal shell windows than do Windows users.
    2. With the latest advances in GUI interfaces to UNIX, particularly with Linux, the overall UNIX user experience can look very much like a Windows experience.
    3. In 357, we'll be dealing with UNIX primarily at the system level, writing C programs that make system calls.
      1. Given this objective, we'll not be spending much, if any time in higher-level application programs where the Windows-like GUI experience is important and useful.
      2. To provide a universal interface to the compilation and execution of C programs, we'll use the command-line interface to the C tools when discussing the labs and programming assignments.
      3. As discussed just below, you are free to use a higher-level IDE to develop your programs, if you like.

  20. The basic C tools for CSC 357.
    1. The GNU C compiler -- gcc.
    2. The GNU C debugger -- gdb.

  21. C development environments.
    1. A plain text editor, a UNIX terminal, and the basic tools.
    2. Emacs and the basic tools (Fisher's environment of choice, money no object).
    3. Open-source IDEs:
      1. Eclipse CDT -- C development tools integrated into the well-known Eclipse environment.
      2. Gnu DDD -- the "Data Display Debugger", that includes general IDE functionality.
      3. jGrasp -- a "lightweight" development environment for Java, C, and other languages.
    4. A slew of commercial IDEs.

  22. IMPORTANT NOTE about the C execution environment for CSC 357.
    1. As stated in the syllabus, all 357 programs must run correctly on the falcon/hornet machines at Poly.
    2. It's fine to use whatever development environment(s) you like, but before you submit your work, you must confirm that it compiles and executes properly on falcon/hornet.



index | lectures | labs | programs | handouts | solutions | examples | documentation | bin