C Programming, Part I X52.9232
Home Page

Here's a syllabus that's more detailed than the catalog description.

A World Wide Web gateway that outputs a flag JPEG

The output of this C program is converted to JPEG format by a gateway that runs programs in the netpbm library. See, for example, the documentation about the ppm file format and its magic number P6. The trios of numbers are the amounts of red, green, and blue that constitute each color. They must be in the range 0 (minimum) to 255 (maximum) inclusive. For example, the trio 128, 128, 128 is gray, and the trio 0, 255, 255 is blue-green. The file /usr/openwin/lib/rgb.txt has more recipes for colors. There’s even a flag database. Here is the same gateway in C++.

GIF (Graphics Interchange Format) sometimes looks better.
JPEG (Joint Photographic Experts Group)

First verify that the above works by pressing the "Compile & run" button without changing any C code. Then change the C code to make the simpler French tricolor: three vertical bands of blue, white, and red. (You may have to press your browser's Reload or Refresh button after you see the flag.) Aux armes, citoyens! Formez vos bataillons!

To print the flag, you may first have to save it on your hard disk by pulling down the File menu and selecting Save As....


Contents

  1. Platforms and compilers
  2. Prerequisites
  3. SCPS catalog description. Other related courses: C Programming, Part II X52.9233 Object-Oriented Programming Using C++, Parts I & II X52.9264 and X52.9265.
  4. Syllabus
  5. Bibliography
  6. C source code
  7. Current list of students
  8. A World Wide Web gateway that draws a flag.

Platforms and compilers

You can use any current version of C on any machine for this course.

Unix

Every student gets a free Unix
account on i5.nyu.edu for the duration of the course. (Students also get NYU DIAL accounts, which must be activated.) i5 has the following compilers. Use the GNU ones:

  GNU DEC
C /usr/local/bin/gcc /usr/ucb/cc
C++ /usr/local/bin/g++

Non-Unix platforms

You can also use any version of C++ on any other platform, but you have to supply it yourself. The most popular are ones Microsoft Visual C++ and Borland C++. Here are hints for these versions of C and C++.

Prerequisites

This is a platform-independent course: we cover only the C language, not the details of each machine and operating system. The student is therefore responsible for discovering how to log onto their machine; how to create, edit, save, and print files; and how to invoke their C compiler.

Syllabus

A computer is a machine that follows instructions. The list of instructions you put into the computer is called a program. In the future, people will write programs in English. But this hasn't happened yet, so for the time being we write in simpler languages such as C or C++.

X52.9232 is an introduction to the C programming language. In this course we will learn C by writing and reading C programs. The following is a general outline for the course, and is subject to change without notice.

Week 1: Everything you need to get started

Printing words and integers (whole numbers) with printf, declaring and initializing variables, operator precedence, inputting an integer with scanf, writing loops with while, for, and do-while, using rand and srand to generate random numbers, if statements, if-then-else, if-then-else vs. switch, and format, spacing and indentation rules.

Week 2: Data types and operators

data types: short, int, long, char, float, double, long double, arrays, expressions, binary operators: order of evaluation, operator precedence, associativity, unary operators, assignment operators, scientific notation, and writing equations.

Week 3: A few pointers

We look at function calls and subscripting as binary operations, then devote ourselves to pointers.

Week 4: String along

We discuss single quotes and double quotes and their uses in printing characters and strings. After looking at output, we turn to input, using getchar and scanf, and learn the difference between strlen and sizeof.

Week 5: Getting bitwise

Using #define to create macros, the numerical value of relational and logical expressons, bitwise "and", "or", "exclusive or", and "not".

Week 6: An array of pointers

The ternary operator ?:, initializing a pointer, looping through an array of pointers, dereferencing a pointer to a pointer.

Week 7: What's your function?

Writing a function, using arguments, return values, call by value and call by reference.

Week 8: Everything you need to get serious

Command line arguments, dynamic memory allocation with malloc, realloc, and free, dividing a program into several .c files, creating header files for function declarations, using extern to extend the scope of a variable across files, intermediate steps in a C compilation: preprocessor, compiler, assembler, linker. We introduce structures, and unions.

Week 9: I/O, I/O, it's off to work we go...

The source of input may be the keyboard, a file, or another program. The destination of output could be the screen, a file, or another program. The standard i/o library provides us with a variety of functions to direct i/o. scanf, getchar, printf, and putchar use the terminal's keyboard and screen as default source and destination, or can use a single source and a single destination as supplied in the command line. fscanf and fprintf allow you to specify a different source or destination each time they are called in your program. sscanf and sprintf read from and write to memory. These and other facets of i/o will be covered this week.

Week 10 & 11: Wow! Section 6 covers linked lists!

Review of data types invovling pointers, building linked lists, deleting nodes, sorted linked lists, doubly linked lists, and a gentle introduction to recursion, the comma operator, enum, and overview: things that happen automatically in others languages, but not in C.

Week 12:

Final.

Bibliography

The first one is the textbook for X52.9232; the rest are recommended. The lectures are accompanied by
Handouts available on the web.
  1. C Programming: A Modern Approach by K. N. King; W. W. Norton & Company, 1996; ISBN 0-393-96945-2. How many copies are left at the NYU Bookstore (not the NYU Computer Bookstore)? It's $56.75 in aisle 9 downstairs. Page numbers in the handouts refer to this book and the following one.
  2. The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie; Prentice-Hall, 1988; ISBN 0-13-110362-8. C++ is a superset of C, and this is the definitive (and shortest) C book. Magisterial but tough going.
  3. The Elements of Programming Style, Second Edition by Brian W. Kernighan and P. J. Plauger; McGraw-Hill, 1978; ISBN 0-07-034207-5. This is the greatest book ever written about programming. In particular, it's the best book ever written about what is now pejoratively called "procedural programming" but which was then called "structured programming".

Source code

To download the following files onto i5.nyu.edu, find the URL of each file by pointing at it in your browser. Then on i5.nyu.edu, let the URL be the second argument of the lynx command. For example,
cd to the directory on i5.nyu.edu to which you want to download a file
pwd
lynx -source http://i5.nyu.edu/~mm64/x52.9264/planet1.c > planet1.c
ls -l planet1.c