Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

C by Example

C by Example

1.0 1
by Greg Perry

See All Formats & Editions

C By Example makes programming easy to learn for the beginning programmer. It builds your programming skills by using clear, easy-to-understand exercises. Hands-on, real world examples show you how to write programs that flow correctly and produce accurate results. At the end of the book, you will use the concepts you have learned to build a blackjack game. Also


C By Example makes programming easy to learn for the beginning programmer. It builds your programming skills by using clear, easy-to-understand exercises. Hands-on, real world examples show you how to write programs that flow correctly and produce accurate results. At the end of the book, you will use the concepts you have learned to build a blackjack game. Also included is an appendix that discusses where to find compilers, other C resources, where to go from here, and what kinds of careers you could go into with the knowledge she gained from the book.

Product Details

Publication date:
By Example Series
Edition description:
Product dimensions:
7.40(w) x 9.00(h) x 1.20(d)

Read an Excerpt

Chapter 1: What C is all About

C 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)...

Meet the Author

Greg Perry is a bestselling author and co-designer of the by Example series. He has written more than 50 computer books, many of which are bestsellers. He has been a programmer, trainer, and speaker for the past 20 years. Previously, Greg taught advanced programming courses at Tulsa Community College. He currently lectures at programming conferences across the country and is fluent in 9 computer languages. Among his bestselling books are Sams Teach Yourself Beginning Programming in 24 Hours, C by Example, Sams Teach Yourself Visual Basic 6 in 21 Days, and Sams Teach Yourself Windows 98 in 24 Hours.

Customer Reviews

Average Review:

Post to your social network


Most Helpful Customer Reviews

See all customer reviews

C by Example 1 out of 5 based on 0 ratings. 1 reviews.
Guest More than 1 year ago
This book is full of errors, and it made me wonder how come this author got so many misconceptions about C. You will, definitely, not learn C from this book. If you want to become a serious programmer in C, consult the 2000 book reviews at ACCU (Association for C and C++ Users) Internet site. If you just want to impress your friend at your first date, make sure your date does not know C better than this author, because this book is just as bad as so many C/C++ books available in the market.