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

What this book covers

Section 1, C Fundamentals, introduces the very basic concepts of C syntax and program structure.

Chapter 1, Running Hello, World!,introduces the program development cycle and the tools you'll need for the rest of the book. Those tools are used to create, build, and run your first C program, a "Hello, world!" program. The concepts of commenting code and experimenting with code are also introduced.

Chapter 2, Understanding Program Structure,introduces statements and blocks. It also describes function definitions and function declarations, also known as function prototypes. How functions are called and their order of execution is illustrated. Statements, blocks, and functions define the structure of C programs.

Chapter 3, Working with Basic Data Types,explores how C represents values in various ways through the use of data types. Each data type has a size and possible range of values that C uses to interpret a value.

Chapter 4, Using Variables and Assignment, introduces variables and constants, which are used to contain values. For a variable to receive a value, that value must be assigned to it; several types of assignment are explained.

Chapter 5, Exploring Operators and Expressions, introduces and demonstrates operations—ways to manipulate values—on each of the various data types.

Chapter 6, Exploring Conditional Program Flow, introduces flow of control statements, which execute one group of statements or another depending upon the result of an expression.

Chapter 7, Exploring Loops and Iteration, introduces each of the looping statements. It also describes the proper and improper use ofgoto. Additional means of controller loop iterations are explained.

Chapter 8, Creating and Using Enumerations, explains named constants, enumerations, and how to use them.

Section 2, Complex Data Types, extends your understanding of the concepts of basic, or intrinsic, data types to more complex types.

Chapter 9, Creating and Using Structures, explores how to represent complex objects with groups of variables, called structures. Operations on structures are explored. How structures are related to object-oriented programming is described.

Chapter 10, Creating Custom Data Types with typedef, describes how to rename enum and struct declarations. Compiler options and header files are explored.

Chapter 11, Working with Arrays, illustrates how to define, initialize, and access simple arrays. Using loops to traverse arrays is explored. Operating on arrays via functions is demonstrated.

Chapter 12, Working with Multi-Dimensional Arrays, extends your understanding of the concept of 1-dimensional arrays to 2, 3, and n-dimensional ones. Declaring, initializing, and accessing these multi-dimensional arrays in loops and in functions are demonstrated.

Chapter 13, Using Pointers, explores direct and indirect addressing with pointers. Operations with pointers are demonstrated. How to think and talk about pointers is described. Using pointers in functions and using pointers to structures is demonstrated.

Chapter 14, Understand Arrays and Pointers, explores the similarities and differences between pointers and arrays.

Chapter 15, Working with Strings, introduces the ASCII character set and C strings, which are arrays with two special properties. A program to print the ASCII character set in a table is developed. The C Standard Library string operations are introduced.

Chapter 16, Creating and Using More Complex Structures, builds upon the concepts of structures and arrays to explore how to create various combinations of complex structures. Throughout the chapter, each complex structure is demonstrated through the development of a complete card dealing program. This chapter provides the most comprehensive example of the method of stepwise, iterative program development.

Section 3, Memory Manipulation, explores how memory is allocated and deallocated in a variety of ways.

Chapter 17, Understanding Memory Allocation and Lifetime, introduces the concepts of automatic versus dynamic memory storage classes as well as internal versus external storage classes. The static storage class is demonstrated.

Chapter 18, Using Dynamic Memory Allocation, introduces the use of dynamic memory and describes various operations on dynamic memory. A dynamic linked-list program is demonstrated. An overview of other dynamic structures is provided.

Section 4, Input and Output, explores a wide variety of topics related to the reading (input) and writing (output) of values.

Chapter 19, Exploring Formatted Output, goes into thorough detail about the various format specifiers of printf() for each of the intrinsic data types: signed and unsigned integers, floats and doubles, and strings and characters.

Chapter 20, Getting Input from the Command Line, demonstrates how to use the argc and argv parameters of main() to get values from the command line.

Chapter 21, Exploring Formatted Input, demonstrates how to read values from an input stream usingscanf(). It clarifies how the format specifiers forprintf()andscanf(), while similar, are really very different. Internal data conversion and unformatted input and output are also demonstrated.

Chapter 22, Working with Files, is a largely conceptual chapter that introduces basic file concepts. It demonstrates how to open and close files from within the program and from the command line.

Chapter 23, Using File Input and File Output, demonstrates how to use command-line switches with getopt() to read and write files. The basic program is then expanded to read names from input, sort them via a linked list, and then write them out in sorted order.

Section 5, Building Blocks for Larger Programs, details how to create and manage programs that consist of multiple files.

Chapter 24, Working with Multi-File Programs, demonstrates how to take the single source file program that was developed in Chapter 16, Creating and Using More Complex Structures, and separate it into multiple source files. Each of the source files has functions that are logically grouped by the structures they manipulate. Effective and safe uses for the preprocessor are described.

Chapter 25, Understanding Scope, defines various components of scope and how they related to single- and multi-file programs. Details of variable scope and function scope are described. The epilogue outlines some useful next steps to take in learning both C and programming.

Appendix, provides a number of useful reference guides. These include C keywords, operator precedence, a summary of some useful GCC and CLang options, ASCII characters, using Bstrlib, an overview of Unicode, and an itemization of the C Standard Library.