On this page:
1.1 Prospective Audience
1.2 Software
1.3 Command-line environments
1.3.1 Macs
1.3.2 Linux machines
1.3.3 Cygwin
1.3.4 Other choices
1.4 Text editors
1.4.1 Dr  Racket
1.4.2 Nano/  Pico
1.4.3 Vim
1.4.4 Emacs
1.5 C compiler
1.6 Optional textbooks
1.6.1 Reading the King book in IYMLC order
7.6

1 Getting started

This chapter is entirely about preparing to learn to program in C: who might want to do it via this document, obtaining and installing software, and optional references. The actual C content starts in the next learning module.

1.1 Prospective Audience

This material was originally in my course CS 146, a second "advanced" course in computer science at the University of Waterloo. Like its predecessor CS 145, I wanted it to be accessible to students who were enthusiastic and had aptitude but had perhaps never programmed before. Because the "regular" course sequence used Racket and then C (this was originally my choice also), students going on in the computer science major needed to know C. But many of my students were Math majors, and there was no point in their learning C. Consequently, I split the C material off into optional tutorials and exercises (and warned the CS majors not to neglect it). It is thus quite ready for self-study.

But it does assume that you have programmed in Racket before, perhaps in a course based on How To Design Programs (HtDP), or through my own flâneries Teach Yourself Racket (TYR) or Functional Introduction to Computer Science (FICS, in progress). The non-C content of my CS 145/146 courses forms the basis of FICS.

The reliance of this material on Racket is not heavy, as the two languages are quite different. It is mostly limited to some analogies and some lamentations about how nice we had it when we were using Racket. If you don’t know Racket, you can probably just skip over those points. And, who knows, it might look intriguing enough to get you to take a look at Racket.

Why did I choose C originally? Many popular imperative languages have quite complicated computational models, which are largely left undiscussed. They tend to be taught by example, which often leads to shallow understanding. There is a lot about C that is annoying, but it at least has a reasonably clear semantic model (even if the standard leaves a disconcerting amount of behaviour "unspecified", mostly to facilitate the work of compilers). In CS 146 (and in the second volume of FICS derived from it), I used toy imperative languages to focus attention on the key issues. But that won’t suffice, politically. Colleagues would accept C.

This flânerie is not as inventive as the others, and it is shorter than them (except for TYR). I had a job to do in creating this material, but I wasn’t passionate about it. But I tried to do a good job for the sake of those willing to engage with it, and I was consistently looking for ways to improve my explanations.

If I had complete freedom to choose a second language not made up by me, I think it would be OCaml, in the context of a suitable set of intermediate material such as the data structures in my flânerie Functional Data Structures (FDS). But you’re presumably here because you’re interested in C. So let’s get on with it.

1.2 Software

There are a number of options available to you for developing C code. But, unlike the situation with Racket, there is no one simple multi-platform solution for C development, especially for the beginner. Below I list the pros and cons of some approaches. You should read them over, then choose a command-line environment and text editor, and go through any necessary installation procedures.

1.3 Command-line environments

A command-line environment (CLE, sometimes also referred to as a shell) is, to use a Racket analogy, something like an Interactions window for the whole operating system. It provides you with a way to compose complex commands using the keyboard instead of pointing and clicking. Text offers much greater abstractions, including the possibility of automating sequences of actions. While it is possible to program in C entirely within a windowed graphical application, it is best to learn how to use a CLE for editing, compiling and running C programs. There is a programming language for shell actions, and I show some brief snippets later on, but there is no need for you to learn it in detail at this point.

There are many ways to use a CLE, and I list the main options below, followed by options for text editors.

1.3.1 Macs

The C compiler is not installed by default on Macs, but you can get it from Apple, either by installing XCode (which is large) from the App Store, or the Command Line Tools (much smaller).

Pros: Macs provide you with a good graphical user interface (GUI) and a good CLE, and you can be very productive using them.

Cons: Relatively few. There used to be many fewer Mac users to consult, but now many people use the platform.

1.3.2 Linux machines

If you own a Windows computer, you can run Linux on it, and some hardware manufacturers offer it preinstalled. You can either install Linux and boot into it, or you can install it on a virtual machine running under Windows.

Pros: You get major karma points for running free open-source software. There are many optional packages available for download providing all sorts of added functionality, and extensive user communities active on the Internet.

Cons: Installing and configuring Linux can be tricky, especially if you want to keep your Windows installation functioning as before. The virtual machine option may be slower, depending on the power of your hardware. There are many different flavours of Linux. Ubuntu is probably the best choice if you don’t already have one in mind.

1.3.3 Cygwin

Cygwin is a system that gives you a Unix-like CLE that can be run in its own window within Windows. For Windows users, this may be the least painful option.

Pros: You don’t have to reboot into a different system, so you can work on this material and quickly switch back and forth to do other familiar tasks.

Cons: The installation of Cygwin is fairly simple but has several possibly confusing steps.

1.3.4 Other choices

Laptops running Android have become an option since this material was developed. Since Android is based on Linux, it should be possible to develop and run C programs, but I have no idea how easy it is to set up. Some of my students in the past have tried to use MinGW, a "minimalist GNU for Windows" package, only to discover that their programs would not compile or would not run as expected with our tests. Since you are studying on your own, this is not going to be an issue, and that may work for you in this context.

1.4 Text editors

You will need a text editor within your choice of CLE to prepare your C programs. Again, there are several choices. I used to strongly recommend that students running Windows not use Wordpad, and under no circumstances attempt to use Microsoft Word. My recommendation for all platforms was Vim, with Nano for the cautious and Emacs for the ambitious. Hardcore CS types tend to split between Vim and Emacs and fight with each other over which is better. This is silly; use the one you want, and let others use the one they want. All of the editors listed below are already installed on Macs, and can be easily installed on Linux (if they are not already) and Cygwin.

1.4.1 DrRacket

DrRacket has the capability of editing text files without treating them as if they are Racket programs. It does this automatically for files ending in .txt, but your C programs will end with .c. Still, you can change the mode of any file edited by DrRacket using the Modes menu item at the bottom of the Edit menu.

Pros: You should already be familiar with DrRacket.

Cons: It will not provide you with any of the support you are used to with Racket programs (syntax highlighting, automatic indentation, and so on). Most of the other editors will do at least some of this. DrRacket is best used for Racket programs, not C programs. We recommend that you try to learn how to use one of the other editors listed.

1.4.2 Nano/Pico

Pico is a simple text editor designed at the University of Washington. Nano is an open-source version of pico developed by the Free Software Foundation. You can consider them as equivalent, and I will just discuss the more recent Nano here. It is started from within a CLE with the command nano, and managed entirely from the keyboard without using a mouse.

Pros: It is very simple to use. At any point, the most likely commands are displayed at the bottom of the screen, including contextual help. When you type, what you type is added to the file you are editing. All commands are given using the "control" key. For example, when you start Nano up, among the commands listed at the bottom is ^X, which is described as "Exit". That means that if you hold down the "control" key and then press the "x" key, you will leave Nano.

Cons: It is perhaps too simple. It provides you with basic practice in using a keyboard-oriented text editor, but you may want to move to one of the next two editors.

1.4.3 Vim

Vim is a modern version of the legendary text editor vi, developed for Unix in the 1970s. In contrast to Nano, it is a modal editor. In insert mode, just about anything you type is recorded as text; to issue commands, you must switch to command mode, at which point most commands can be issued with one or two keystrokes. You start it by typing vim in the CLE (it tells you how to exit when it starts, so pay attention).

Pros: Once you get used to modality, Vim can be very fast to use, especially if you are a good touch typist. It does a certain amount of autoindenting of C code, and if your terminal emulator supports colour (or if you download one of the graphical versions for your platform), it can do syntax highlighting. There is a good tutorial built into the program itself.

Cons: Since just about every key on the keyboard is used for some command or other, using Vim can be counterintuitive, and you may find your progress slow at first.

1.4.4 Emacs

Emacs is a very powerful text editor which is highly reconfigurable through an extension language which is a dialect of Lisp (and so of particular interest to those who know Racket). It is not modal in the way Vim is; text is just typed in, while commands are issued using modifier keys such as control, option, and escape.

Pros: You can do just about anything in Emacs, and set it up just the way you like. It has terrific support for programming in many languages. Invoking it in a windowed environment lets you use multiple windows, colours, and the mouse. It even has a mode that is very close to Vim. Many basic Emacs editing commands also work in Mac applications, in DrRacket, and in the CLE shells for retrieving and editing past commands. There is also a tutorial built into Emacs.

Cons: Emacs is huge, and you probably won’t use 99% of it. The stock criticism of it is "Nice operating system, shame about the text editor". It can take quite a while to get used to it enough to use it efficiently, and you are likely to be distracted into learning about it for its own sake. I recommend that if you can, try it out in a low-cost way before committing to it.

1.5 C compiler

The C compiler referred to here is clang. If you have your own Mac, you can install Xcode or the Command Line Tools, as described above, to get it. All versions of Linux either pre-install it or let you install it over the Internet. It is an optional install for Cygwin.

1.6 Optional textbooks

I do not know of a free online reference that I consider a complete C learning solution. Textbooks can help, but there are many bad textbooks out there. Since this material was optional in my original course, so were the recommended textbooks. Textbooks are expensive, and even students who obtain one for less cost often do not read it unless they are stuck deep in an exercise, at which point it is usually too late. So the material here is largely self-contained, but can be significantly enhanced by working with one of the two options below.

The shorter and less expensive one, "The C Programming Language" by Kernighan and Ritchie, is a classic text, but it is terse and does not contain details of modern standards. It is an option for good programmers who are willing to work at reading it, but it also takes work to mesh with the approach here.

Not surprisingly, the textbook ("C Programming: A Modern Approach" by K.N. King) that I chose to support the coverage of programming in C in our CS 136 course was not written for students who have had a first course using Racket. It is written in a self-contained manner, assuming students either have had no experience with computing, or have had experience with another imperative language. For students with only Racket experience, some parts will seem too easy, and others somewhat mysterious. The chapters have to be read out of order, which causes further complications.

The next sections list ways to match the King textbook with this material. If you don’t plan to use the book, you can skip it.

1.6.1 Reading the King book in IYMLC order

IYMLC 02: Introduction

Read chapter 1. Note that this flânerie uses the C99 standard.

Read chapter 2 (fundamentals). In 2.3, note the two ways to include comments in your programs.

I avoided the use of scanf as described in 2.5, and there is associated "do this, it just works" syntax that is mysterious at this point. Still, reading this helps you to understand later examples.

I also avoided the use of #define as described in 2.6, instead using constant declarations as described at the beginning of 18.3.

Chapter 3 (formatted I/O) is optional. Come back to this to look things up as you need to.

Read chapter 4 (expressions). You don’t need to learn all the operators at once.

Read 5.1 (logical expressions) and 5.2 (if). Note that I use C99’s Boolean values. The coverage of logical expressions here is very brief, so this is where you should pick up details.

Reach Chapter 9 (functions). Some of the examples involve loops (Chapter 6) and arrays (Chapter 8). You can either skip them, or read those chapters now, as the concepts aren’t difficult and you’ve probably seen them before.

Prototypes are discussed in 9.2 (function declarations) and header files in 15.2.

Read 7.1, 7.2, 7.3 (basic types). 7.5 is not used here but you should probably read it.

Read 6.1 (while), 6.3 (for), 6.4 (break and continue).

IYMLC 03: Pointers

Read Chapter 11 (pointers), 16.1, 16.2 (basics of structures), 17.5 (linked lists), 17.4 (free), 17.6 (pointers to pointers).

16.4 (unions) is optional.

Read 8.1 (one-dimensional arrays), if you haven’t already. 8.2 (multi-dimensional arrays) is optional. Those who enjoyed learning linear algebra might find it useful.

IYMLC 04: Intermediate C

Read Chapter 13 (strings), 17.1, 17.2, 17.3 (remember my warning in these notes about realloc), 12.1 (pointer arithmetic), 12.2 (using pointers for array processing), 12.3 (array names and pointers), 16.3 (nested arrays and structures).

12.4 (pointers and multidimensional arrays) is optional.

18.1 (declaration syntax) and 18.2 (storage classes) are optional.

Those who plan to use C or even just put it on their CV should read 7.4 (type conversion), 7.5 (type definitions), and Chapters 10 (program organization), 19 (program design, including a look at C++), 20 (low-level programming), and 22 (I/O).

Chapters 14 (the preprocessor) and 15 (writing large programs) are optional eventual reading.