Book Image

Learn C Programming - Second Edition

By : Jeff Szuhay
Book Image

Learn C Programming - Second Edition

By: Jeff Szuhay

Overview of this book

The foundation for many modern programming languages such as C++, C#, JavaScript, and Go, C is widely used as a system programming language as well as for embedded systems and high-performance computing. With this book, you'll be able to get up to speed with C in no time. The book takes you through basic programming concepts and shows you how to implement them in the C programming language. Throughout the book, you’ll create and run programs that demonstrate essential C concepts, such as program structure with functions, control structures such as loops and conditional statements, and complex data structures. As you make progress, you’ll get to grips with in-code documentation, testing, and validation methods. This new edition expands upon the use of enumerations, arrays, and additional C features, and provides two working programs based on the code used in the book. What's more, this book uses the method of intentional failure, where you'll develop a working program and then purposely break it to see what happens, thereby learning how to recognize possible mistakes when they happen. By the end of this C programming book, you’ll have developed basic programming skills in C that can be easily applied to other programming languages and have gained a solid foundation for you to build on as a programmer.
Table of Contents (37 chapters)
Part 1: C Fundamentals
Part 2: Complex Data Types
Part 3: Memory Manipulation
Part 4: Input and Output
Part 5: Building Blocks for Larger Programs

To get the most out of this book

To use this book, you will need a basic text editor, a terminal or console application, and a compiler. Descriptions of each of these and how to download and use them are provided in Chapter 1, Running Hello, World!. Here are the technical requirements for this book:

All of the software given in the table are either built into the operating system or are free to download.

To install GCC on certain Linux OSs, follow these steps:

  • If you are running an RPM-based Linux, such as Red Hat, Fedora, or CentOS, on the command line in Terminal, enter the following:
    $ sudo yum group install development-tools

If you are running Debian Linux, on the command line in Terminal, enter the following:

$ sudo apt-get install build-essential				
  • To verify your installation of GCC or Clang for any platform, on the command line in Terminal, enter the following:
    $ cc --version				

Whichever version of this book you are using, digital or hard copy, we advise you to type the code yourself. After you do that, you can access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

If you are an absolute beginner, once you have the necessary development tools, you will need to learn how to read a programming book. If you have taken an algebra course or a calculus course in school, then you will need to approach learning from a programming book in a similar fashion:

  1. Read through the chapter to get an overview of the concepts being presented.
  2. Begin the chapter again, this time typing in each program as you encounter it. Make sure you get the expected output before moving on. If you don't get the expected output, try to figure out what is different in your program from the one given. Learning to program is a lot like learning math – you must do the exercises and get the programs to work. You cannot learn to program just by looking at programs; to learn to program, you must program. There is no way around that.
  3. Focus on memorizing keywords and syntax. This will greatly speed up your learning time.
  4. Be aware that you will need to sharpen the precision of your thinking. The syntax of computer language is extremely precise, and you will need to pay extra attention to it. You will also have to think much more precisely and in sometimes excruciating detail about the steps needed to solve a particular problem.
  5. Review both the concepts and example programs. Make a note of anything you don't understand.

If you are an experienced programmer who is new to C, I still strongly advise you to first skim the text and examples. Then, enter the programs and get them to work on your system. This will help you to learn C syntax and its idioms more quickly.

I have found that it is important to understand what kind of book you are reading so that you can use it most appropriately. There are several kinds of computer programming books:

  • Conceptual books, which deal with the underlying ideas and motivation for the topics they present. Kernighan and Ritchie's The C Programming Language and Effective C: An Introduction to Professional C Programming, by Seacord, are two such books.
  • Textbooks, which go through every major area of the language, sometimes in gory detail and usually with a lot of code snippets. Deitel and Deitel's books, as well as C Programming: A Modern Approach, by K. N. King, are examples of these. They are often best used in a formal programming course.
  • Reference books, which describe the specifics of each syntax element. C: A Reference Manual, by Harbison and Steele, is one such book.
  • Cookbooks, which present specific solutions to specific problems in a given language. Advanced C Programming by Example, by Perry, Expert C Programming: Deep Secrets, by Van Der Linden, and Algorithms in C, by Sedgewick, are examples of these.
  • Topical books, which delve deeply into one or more aspects of a programing language. Pointers in C, by Reek, is one example.
  • Practice books, which deal with how to address programming with C generally. C Interfaces and Implementations, by Hanson, and 21st Century C: C Tips from the New School, by Klemens, are two examples of these.

There are different ways to use these books. For instance, read a conceptual book once, but keep a reference book around and use it often. Try to find cookbooks that offer the kinds of programs you are likely to need and use them as needed.

I think of this book as a combination of a C cookbook, a C reference book, and a C practice book. This is not intended to be a textbook. All of the programs are working examples that can be used to verify how your compiler behaves on your system. Enough of the C language has been included that it may also be used as a first-approximation reference. Throughout, my intent has been to show good programming practice with C.

I expect that Learn C Programming will not be your last book on C. When you consider other C books, be sure that they pertain to C99 at a minimum; ideally, they should include C11, C17, or C23. Most C code before C99 is definitely old-school; more effective programming practices and methods have been developed since C99 and before.