Read an Excerpt
Chapter 1: What C is all AboutC Is Fundamental
Just a few years ago, C was the most popular programming language being used. Programmers wrote C code for PCs, mainframes, and supercomputers. C was standardized so that the same program would run on every kind of computer using every kind of operating system available.
Today, the use of C has dramatically decreased. Rarely will a project be written in C. Programmers are using languages such as C++ and Java to create applications that run on standalone, networked, and Internet-based machines. Yet, when learning to program, C is considered to be the one language it is imperative a programmer master before moving on to the other languages in use today.
Due to the Internet's phenomenal growth, new languages are appearing to handle the different challenges that Internet-based processing requires. Companies looking for programmers in these newer, and sometimes obscure, languages often advertise for C programming skills. The companies know that someone well-versed in C can pick up these offshoot languages rapidly. Therefore, learning C will boost your career potential even if you are never hired to write C-based code.
The reason for C's recommended mastery, despite its low levels of actual use, is that C is the common denominator of many of today's languages; learn C and these languages will be simple to learn. Languages such as C++ and Java are based on C. In fact, many of the statements and commands in these newer languages are identical to the ones found in C. Overall, C is much simpler to learn than these languages because it carries with it a much slimmer toolkit of add-on procedures.
When some people attempt to learn C, even if they are programmers in other lan- guages, they find that C can be cryptic and difficult to understand. This does not have to be the case. When you are taught to write clear and concise C code, in an order that builds on fundamental programming concepts, C is no more difficult to learn or use than any other programming language.
The History of C
Before you jump into C, you might find it helpful to know a little about the evolution of the C programming language. Bell Labs first developed this language in the early 1970s, primarily so that Bell programmers could write their UNIX operating system for a new DEC (Digital Equipment Corporation) computer. Bell Labs designed UNIX to run efficiently on small equipment, and it was the first operating system to be written entirely in a high-level programming language. Until that time, operating systems were written in assembly language, the computer's low-level, hardware-based language that is tedious, time-consuming, and difficult to change. The Bell Labs designers knew they needed a higher-level programming language to implement their project quicker and make its code easier to maintain.
Because other high-level languages at the time (COBOL, FORTRAN, PL/I, and Algol) were too slow to use for an operating system's code, the Bell Labs programmers decided to write their own language. They based their new language on Algol and BCPL, two high-level but efficient languages used throughout the European markets, but rarely in America. BCPL strongly influenced C, although BCPL did not offer the various data types that the makers of C required. After a few versions, these Bell programmers developed a language that met their goals very well. C is efficient (it is sometimes called a high low-level language due to its speed of execution) and flexible, and contains the proper constructs enabling it to be maintained over time.
How C Differs
If you have programmed before, you should understand a little about how C differs from other programming languages on the market. (Today's new Cbased languages, such as Java, also contain many of C's nuances.) Besides being a very efficient language, C is known also as a weakly typed language; that is, the data types you assign to variables do not necessarily require that same type of data. (Proper coding techniques learned in this book, however, will help to eliminate this problem.) If you declare a numeric variable, and then decide to put a letter into it, C enables you to do this. The data may not be in the format you expect, but C does its best. This is much different than stronger-typed languages such as COBOL and Pascal. These languages require rigid conformity to consistent data types; you cannot store, for example, a character in a numeric storage location. Although you can get into trouble a little more easily, C enables you to view the same data in different ways.
C's weakly typed nature places much more responsibility on the programmer. C is an extremely flexible language-particularly if it is to be used to write operating systems. At any one time, an operating system does not know what is coming down the line. If, for example, an operating system expects a number but instead receives a letter, the language used must be flexible enough to handle this different data without aborting.
programmer has more freedom with data storage, the languages do not check data type accuracy for the programmer. The trade-off is worth it, however. The designers of C did not want to hamper C programmers by adding lots of strict rules to the language.
C is a small, block-structured programming language. C has fewer than 40 keywords. To make up for its small vocabulary, C has one of the largest assortments of operators, such as those used for calculations and data comparisons. (The C-based languages have more operators than virtually every other language in existence, second only to AIPL.) The large number of operators in C could tempt programmers to write cryptic programs that do a lot with a small amount of code. As you learn throughout this book, however, making the program more readable is more important than squeezing out bytes. This book teaches you how to use the C operators to their fullest extent, while maintaining readable programs.
C's large number of operators (more than the number of keywords) requires a more judicious use of an operator precedence table that states the order in which C processes multiple operators inside a single statement.
V Appendix B, "C's Precedence Table," page 476, contains Cs operator precedence table that you can refer to as you learn C.
Unlike most other languages that have only four or five levels of precedence, C has 15. As you learn C, you need to master each of these 15 levels. The precedence table is not as difficult as it sounds, but its importance cannot be overstated.
C also has no input or output statements. (You might want to read that sentence again!) C has no commands that perform input or output. This is one of the most important reasons why C and C-based languages are available on so many different computers. The I/O (input and output) statements of most languages tie those languages to specific hardware. QBasic, for instance, has almost 20 1/0 commands-some of which write to the screen, to the printer, to a modem, and so on. If you write a QBasic program for a microcomputer, chances are sky-high that the program cannot run on a mainframe.
C's input and output is performed through the abundant use of function calls. With every C compiler comes a library of standard 1/0 functions that your program calls to perform input and output. These standard routines are hardware independent, because they work on any device and on any computer that conforms to the ANSI C standard (as most do)...