Book Image

Learn C Programming

By : Jeff Szuhay
Book Image

Learn C Programming

By: Jeff Szuhay

Overview of this book

C is a powerful general-purpose programming language that is excellent for beginners to learn. This book will introduce you to computer programming and software development using C. If you're an experienced developer, this book will help you to become familiar with the C programming language. This C programming book takes you through basic programming concepts and shows you how to implement them in C. Throughout the book, you'll create and run programs that make use of one or more C concepts, such as program structure with functions, data types, and conditional statements. You'll also see how to use looping and iteration, arrays, pointers, and strings. As you make progress, you'll cover code documentation, testing and validation methods, basic input/output, and how to write complete programs in C. By the end of the book, you'll have developed basic programming skills in C, that you can apply to other programming languages and will develop a solid foundation for you to advance as a programmer.
Table of Contents (33 chapters)
1
Section 1: C Fundamentals
10
Section 2: Complex Data Types
19
Section 3: Memory Manipulation
22
Section 4: Input and Output
28
Section 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:

Operating System Cost Download URL
Linux/Unix
Text Editor(choose one)
Nano Free https://www.nano-editor.org/download.php
Vim or vi <Built-in> N/A
GEdit <Built-in> https://wiki.gnome.org/Apps/Gedit
Emacs Free https://www.gnu.org/software/emacs/download.html
Compiler (Installation based on version of Linux/Unix)
GCC <Built-in>

https://gcc.gnu.org/install/ (see the notes following this table for certain Linux versions)

Terminal
Terminal <Built-in> N/A
macOS
Text Editor(choose one)
Vim or vi <Built-in> N/A
emacs Free https://www.gnu.org/software/emacs/download.html
Bbedit Free https://www.barebones.com/products/bbedit/
Compiler
Clang <Built-in>
Terminal
terminal.app <Built-in> N/A
Windows
Text Editor(choose one)
Notepad <Built-in> N/A
Notepad++ Free https://notepad-plus-plus.org/downloads/
emacs Free https://www.gnu.org/software/emacs/download.html
Compiler
Cygwin Free http://www.cygwin.com
MinGW Free http://mingw-w64.org
Terminal
Console <Built-in> N/A

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

  • If you are running an RPM-based Linux, such as RedHat, 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 the 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 tothe 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—youmustdo 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 upon memorizing keywords and syntax. This will greatly speed up your learning time.
  1. Be aware that you will need to sharpen the precision of your thinking. Computer language syntax 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.
  2. 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 in the most appropriate way. 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'sThe C Programming Languageis one such book.
  • Textbooksthat 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 asC 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, andAlgorithms 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, and21st 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. 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 afirst approximationreference. Throughout, my intent has been to show good programming practice with C.

I would expect that Learn C Programmingwill 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 or C18. Most C code before C99 is definitely old school; more effective programming practices and methods have been developed since before C99.