Introduction

The C programming language was first invented in the early 1970s by Dennis Ritchie, an employee at Bell Labs. He created C as an outgrowth of the existing B programming language (in case you were wondering where the weird name came from). Despite the fact that C has been around for over 30 years and that newer languages have come along to improve upon C's functionality, C is still commonly used by many types of programmers.

Although C was written as a language specifically for programmers (unlike Pascal, which was designed to teach programming), even those without formal training or only modest computer skills can pick up the subject. This book was written as the no-frills, here's-what-you-really-need-to-know beginner's guide to the C programming language. Absolutely no programming experience is expected of you, but by following the examples and explanations we present here, you'll have some real-world know-how in no time.

Why Use C?

Numerous programming languages are available today, including C#, C++, and Java. Every language has its strengths and weaknesses, so you might well wonder how C stacks up before investing your time in learning it.

The primary benefits of programming with C are

  • You can create compact, powerful programs.

  • C applications tend to be comparatively fast.

  • You can fine-tune your applications for optimal performance and memory usage.

  • The C code itself is highly portable from one computer to the next.

  • Many popular applications as well as parts of some operating systems have been written in C.

  • An understanding of C is an easy way to gain appreciation for true programming in general and a good first step toward learning C++ and C#, in particular.

On the other hand, three of the more common issues people have with C are

  • C code can be difficult to debug.

  • C does not protect the programmer from making mistakes, nor does C automatically perform maintenance routines.

  • The language lacks more contemporary features such as object-oriented programming.

Regardless of these relatively minor deficiencies, C is a great language with which to familiarize yourself. And, with the right book—like the one you're reading now—you'll be programming with C before you know it.

How C Works

Creating applications in C is a multistep process. First you must understand what the end result is: what your application should do. By clearly stating your objectives, you can better determine your variable needs, what functionality must be present, and so forth. Every application in the book is accompanied by a brief description of its purpose.

The next step is to begin creating the C source code, a plain-text file that will look like Script i.1, for starters. Obviously the main focus of this book is to teach you what code you need to type to create the desired application.

Script i.1. The format of a most basic C source file.


Once you've created your source code, it needs to be compiled. Compilation is the process of turning a plain-text file of C code into a set of computer-readable instructions. The result of the compilation process—assuming it works—is an executable application, compiled specifically to work on that machine (Figure i.1).

Figure i.1. The hello application (on the right) is the compiled result of the hello.c file (on the left).


If compilation did not work, you'll need to debug, debug, and debug. Errors can occur by inadvertently misspelling something, omitting a particular character, or out-and-out misusing a function. Rest assured that all of the code in this book has been debugged, meaning that if you follow the instructions exactly, your code will work too. But, the fact of the matter is that debugging is a good and necessary skill to have.

Once you have a compiled application, you can run it just as you would any other application: by double-clicking on its icon. All of the examples in this book will run in a console or terminal window (Figure i.2).

Figure i.2. Running the hello program on Windows.


What You'll Need

The requirements for working with C are both free and minimal. For starters, you'll need a computer, but you probably already knew that. It doesn't matter what kind of computer you have, what operating system it's running, or really how much memory and hard disk space is available. If your computer can run, say, Microsoft Office, it has all the power you need for creating applications in C.

The most important—in fact, the only—requirement is that you have a qualified C compiler on your computer. This can be as simple as gcc (Gnu C Compiler), which is freely available, can run on most operating systems, and already comes on most Unix-derivative operating systems. Along with yourcompiler, you'll want a text editor and a command-line interface from which you'll run the compiler. Both of these tools are already present on every operating system.

Understanding the C Standards

The C language is a standardized technology, meaning that officially sanctioned versions are available. The first formal C standard was adopted by the American National Standards Institute (ANSI) in 1989. The International Organization for Standardization (ISO) approved a very similar version in 1990. Together, these are called C90.

Modifications to this standard eventually resulted in the C99 standard, which was adopted in 1999 (hence the name). The C99 standard supports a few new data types, works better with international languages, and covers up some gaps in C's functionality.

The standard you adhere to matters since different compilers and IDEs support different versions of C. The three primary applications used in this book—Dev-C++ on Windows, Xcode on Mac OS X, and gcc—all currently support the C99 standard. In other words, if you follow C99 rules, your applications should work.

For increased backwards compatibility, you can adhere to the stricter C90 standard. With this in mind, features that are new to the C99 standard will be described as such.


Although you can use a text editor and a compiler to create your applications, a much easier option is to use an all-in-one integrated development environment (IDE) like Dev-C++ on Windows or Xcode on Mac OS X. Both of these tools are free and allow you to write, compile, debug, and execute your C code all within the one interface. Appendix A, “Installing and Using C Tools,” covers the installation and basic usage of these tools, and we highly recommend you use either Dev-C++ or Xcode, depending on your operating system. Every one of the book's applications conforms to the C99 standard (see the sidebar) and has been tested using both IDEs. If you use either Dev-C++ or Xcode, following the examples will go more smoothly.

From you, the reader, nothing is expected except an interest and willingness to learn C.

About This Book

This book attempts to convey the fundamentals of programming with C, without going into overwhelming details or bombarding you with irrelevant technicalities. It uses the following conventions to do.

The step-by-step instructions indicate what code you are to type or what other steps you are to take. The specific text you should type is printed in a unique type style to separate it from the main text. For example:

printf ("Hello, world!");

Because the column width in this book is narrower than the common text editor or IDE, some lines of code printed in the steps have to be broken where they would not otherwise break in an editor. A small gray arrow indicates when this kind of break occurs. For example:

printf ("Hello, world! How are you doing
 on this rainy Saturday afternoon?");

With such code, you should continue to use one line in your scripts, or else you might encounter errors.

The complete C code is also written as its own separate script and is numbered by line for reference (see Script i.1). You shouldn't insert these numbers yourself, because doing so will render your code unusable. Most good text editors and IDEs will number lines for you. In these script blocks, we also highlight in bold the sections that demonstrate new or relevant concepts.

You will also frequently see images showing the results of running an application (see Figure i.2), displaying a command you need to enter (Figure i.3), or demonstrating a particular subpart of an application (Figure i.4). All of the images were taken on either Windows or Mac OS X (the Mac OS X images and steps are similar to those for a Linux user). The exact appearance of an application will change from one computer to the next, but most of the content should be similar, as will all of the functionality.

Figure i.3. Invoking the gcc compiler in a Mac OS X Terminal window.


Figure i.4. Some images focus on the user interface.


Getting Help

Although this book was written with the intent of being the most down-to-earth, basic, get-going-now text around, you may run into problems and desire a little assistance on occasion. If you'd like some help regarding the content of this book, or C in general, you have options. Here are some choices, in order of likelihood for getting a fast response (fastest options are listed first):

  • Search the Internet.

    If you have questions about a particular function, header file, or concept, Google will often get you immediate answers.

  • Use a C newsgroup or forum.

    Appendix B, “Resources,” lists a number of available outlets for when you have a specific question. If you ask a question wisely (see the sidebar), you should get the answer you need in a relatively short time.

  • Check out the book's supporting Web site.

    The book's official Web site can be found at www.DMCInsights.com/cvqs. There you'll find all the scripts from the book, links to other resources, and a list of any printing errors.

  • Check out the book's support forum.

    At www.DMCInsights.com/phorum/list.php?f=11, readers can post questions, get answers, see what others are doing, and so forth. We (the authors of this book) moderate this forum, which means we'll answer your question if someone doesn't beat us to the punch.

  • Email the authors.

    If all else fails, we gladly welcome your emails at [email protected]. A few words of caution, though: we can't do your job for you, we can't debug the 200 lines of code you wrote overnight, and it will take several days for us to get back to you. Still, if you do write, we will respond, and will try to assist as best as possible.

Asking Questions the Smart Way

Whether you're posting a message to the book's support forum, sending us an email, or asking a question in a newsgroup, knowing how to most effectively ask a question improves the quality of the response you'll receive as well as the speed with which you'll get your answer. To receive the best answer in the shortest amount of time, follow these steps:

1.
Search the Internet, read the manuals, and browse any applicable documentation.

2.
Ask your question in the most appropriate forum (newsgroup, mailing list, and so on).

3.
Use a clear and concise subject.

4.
Describe your problem in detail, show any relevant code, describe what went wrong, include what operating system you're running, and say what development environment (IDE, compiler, etc.) you're using.

For more tips and an enlightening read, see Eric Steven Raymond's “How to Ask Questions the Smart Way” at www.catb.org/~esr/faqs/smart-questions.html. The 10 minutes you spend on it will save you hours in the future!


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset