L earning a programming language is like trying to eat an entire banquet in one bite: You have so much to swallow at once, even to understand the most basic stuff, that it isnât a question of where to start, but rather what not to eat so that you donât get too sick too quickly.
This chapter provides a quick and dirty introduction to a single C language program. The chapter doesnât even explain why things are necessary because, honestly, at this point in the game youâre probably more interested in accomplishing something than truly figuring out why something works. Donât worry: That comes later. For now, this chapter offers a small taste of the feast to come.
The Section Where the Author Cannot Resist Describing the History of C
In the beginning was Charles Babbage and his Analytical Engine, a machine he built in 1822 that could be programmed to carry out different computations. Move forward more than 100 years, where the U.S. government in 1942 used concepts from Babbageâs engine to create the ENIAC, the first modern computer.
To program Babbageâs computer, you had to literally replace stacks of gears. To make the ENIAC carry out different tasks, it had to be rewired by hand.
By the early 1950s, computer programming had evolved from rewiring to entering instructions using rows of switches. The process began with Professor John von Neumann, who in 1945 developed the concept of the function (or subroutine), the IF-THEN choice statement, and the repeating FOR loop. By 1949, Von Neumann had come up with a binary programming language he called Short Code. Then, in 1951, Grace Hopper developed the first compiler, which allowed the computer to be programmed using words and symbols rather than binary ones and zeroes. Computers could then be programmed with written instructions rather than by rewiring or throwing switches.
In the mid-1950s, the first major programming language appeared. Named FORTRAN, for formula translating system, it incorporated variables and introduced logical conditions to the art of programming. Then, in 1959, the COBOL programming language was developed as businesses began to adopt computers. COBOL was the first programming language with a truly English-like grammar.
The Algol language was developed at about the same time as COBOL. From Algol, many programming techniques were introduced that are still used today.
In 1968, Zurich professor Niklaus Wirth used Algol as a basis for the Pascal programming language. Pascal was designed as a teaching tool because it didnât allow for sloppy programming and forced users to follow the rules of structured programming.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working with two languages: B (for Bell) and BCPL (Basic Combined Programming Language). Inspired by Pascal, Mr. Ritchie developed the C programming language.
The C language was used to write the Unix operating system. Ever since that first version of Unix in the early 1970s, a C compiler has always been a part of the operating system â even with Unix variations like Linux and Mac OS. It also explains why Unix comes with so many programming utilities. (Indeed, Unix is often called the âprogrammerâs operating system.â)
In 1983, C programmer Bjarne Stoustroup developed object oriented programming (OOP) extensions to the C language and created the C++ programming language. Even though itâs often taught as a separate subject, C++ is really about 95 percent original C. Even so, some purists stick with the original C language and donât bother to discover that extra 5 percent of C++ â despite the power and flexibility it offers.
Branching off from C in the 1990s was another programming language: Java, from Sun Microsystems. Originally developed for interactive TV, the Java language truly found a home on the Web in 1994 and has been a popular Web programming language ever since.
Time to Program!
The C language has a certain structure to it â a form, a visual look, a feeling. Unlike in more freeform languages, you must obey traditions and rules to put together the most basic of C programs. Thatâs what gives the C language its look and feel.
The following sections introduce you to the basic structure of a simple C program â the skeleton. Each section builds on the next, so read them in order.
Ensure that you have read through Appendix A, which discusses how to set up the C language compiler on your computer and the basic steps for editing, compiling, and running a program.
One suggestion: Please save all the source code files for this book in the basic folder on your hard drive (prog/c/basic).
The basic, simplest C program
When you program a computer, you tell it exactly what to do. The instructions are given in a particular language â in this case, the C language. Those instructions are then compiled into object code. The object code is then linked with a C language library, and the result is an executable file or a program you can run on your computer. Itâs just like magic!
I save the boring specifics for the next chapter. For now, consider the most basic of all programs:
Yes, thatâs a blan...