You are previewing Learn to Program with C.
O'Reilly logo
Learn to Program with C

Book Description

Learn the fundamental programming principles using C, one of the most widely used programming languages in the world today. Learn to Program with C teaches computer programming to the complete beginner using the native C language. As such, it assumes you have no knowledge whatsoever about programming.

We discuss only those features and statements in C that are necessary to achieve our goal. Once you learn the principles well, they can be applied to any language. If you are worried that you are not good at high-school mathematics, don’t be. It is a myth that you must be good at mathematics to learn programming.

C is considered a ‘modern’ language even though its roots date back to the 1970s. Originally, C was designed for writing ‘systems’ programs—things like operating systems, editors, compilers, assemblers and input/output utility programs. But, today, C is used for writing all kinds of applications as well—word processing programs, spreadsheet programs, database management programs, accounting programs, games, robots, embedded systems/electronics (i.e., Arduino), educational software—the list is endless.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgements
  10. Preface
  11. Chapter 1: Elementary Programming Concepts
    1. 1.1 Programs, Languages, and Compilers
    2. 1.2 How a Computer Solves a Problem
      1. 1.2.1 Define the Problem
      2. 1.2.2 Analyze the Problem
      3. 1.2.3 Develop an Algorithm to Solve the Problem
      4. 1.2.4 Write the Program for the Algorithm
      5. 1.2.5 Test and Debug the Program
      6. 1.2.6 Document the Program
      7. 1.2.7 Maintain the Program
    3. 1.3 How a Computer Executes a Program
    4. 1.4 Data Types
    5. 1.5 Characters
    6. 1.6 Welcome to C Programming
      1. 1.6.1 Run the Program
      2. 1.6.2 A Word on Program Layout
    7. 1.7 Write Output with printf
      1. 1.7.1 The Newline Character, \n (backslash n)
      2. 1.7.2 Escape Sequences
      3. 1.7.3 Print the Value of a Variable
    8. 1.8 Comments
    9. 1.9 Programming with Variables
  12. Chapter 2: C – The Basics
    1. 2.1 Introduction
    2. 2.2 The C Alphabet
    3. 2.3 C Tokens
      1. 2.3.1 Spacing Within a Program
      2. 2.3.2 Reserved Words
      3. 2.3.3 Identifiers
      4. 2.3.4 Some Naming Conventions
    4. 2.4 Basic Data Types
    5. 2.5 Integer Numbers - int
      1. 2.5.1 Declaring Variables
      2. 2.5.2 Integer Expressions
      3. 2.5.3 Precedence of Operators
      4. 2.5.4 Print an Integer Using a “Field Width”
    6. 2.6 Floating-Point Numbers – float and double
      1. 2.6.1 Print double and float Variables
      2. 2.6.2 Assignment Between double and float
      3. 2.6.3 Floating-Point Expressions
      4. 2.6.4 Expressions with Integer and Floating-Point Values
      5. 2.6.5 Assigning double/float to int
    7. 2.7 Strings
    8. 2.8 The Assignment Statement
    9. 2.9 printf
  13. Chapter 3: Programs with Sequence Logic
    1. 3.1 Introduction
    2. 3.2 Read Data Supplied by a User
    3. 3.3 scanf
      1. 3.3.1 Read Data Into a float Variable
      2. 3.3.2 Read Data Into a double Variable
    4. 3.4 Read Strings
    5. 3.5 Examples
      1. 3.5.1 Problem 1 - Average
      2. 3.5.2 Problem 2 - Square
      3. 3.5.3 Problem 3 - Banking
      4. 3.5.4 Problem 4 – Tickets
  14. Chapter 4: Programs with Selection Logic
    1. 4.1 Introduction
    2. 4.2 Boolean Expressions
      1. 4.2.1 AND, &
      2. 4.2.2 OR, ||
      3. 4.2.3 NOT, !
    3. 4.3 The if Construct
      1. 4.3.1 Find the Sum of Two Lengths
    4. 4.4 The if...else Construct
      1. 4.4.1 Calculate Pay
    5. 4.5 On Program Testing
    6. 4.6 Symbolic Constants
      1. 4.6.1 The #define Directive
      2. 4.6.2 Example – Symbolic Constants
    7. 4.7 More Examples
      1. 4.7.1 Print a Letter Grade
      2. 4.7.2 Classify a Triangle
  15. Chapter 5: Programs with Repetition Logic
    1. 5.1 Introduction
    2. 5.2 The while Construct
      1. 5.2.1 Highest Common Factor
    3. 5.3 Keep a Count
      1. 5.3.1 Find Average
    4. 5.4 Increment and Decrement Operators
    5. 5.5 Assignment Operators
    6. 5.6 Find Largest
    7. 5.7 Find Smallest
    8. 5.8 Read Data from a File
      1. 5.8.1 fscanf
      2. 5.8.2 Find Average of Numbers in a File
    9. 5.9 Send Output to a File
      1. 5.9.1 fprintf
    10. 5.10 Payroll
    11. 5.11 The for Construct
      1. 5.11.1 The for Statement in C
      2. 5.11.2 A Bit of Aesthetics
    12. 5.12 Multiplication Tables
    13. 5.13 Temperature Conversion Table
    14. 5.14 Expressive Power of for
    15. 5.15 The do...while Statement
      1. 5.15.1 Highest Common Factor
      2. 5.15.2 Interest at the Bank
  16. Chapter 6: Characters
    1. 6.1 Character Sets
    2. 6.2 Character Constants and Values
    3. 6.3 The Type char
    4. 6.4 Characters in Arithmetic Expressions
      1. 6.4.1 Uppercase To/From Lowercase
    5. 6.5 Read and Print Characters
    6. 6.6 Count Characters
      1. 6.6.1 Count Characters in a Line
    7. 6.7 Count Blanks in a Line of Data
    8. 6.8 Compare Characters
    9. 6.9 Read Characters from a File
    10. 6.10 Write Characters to a File
      1. 6.10.1 Echo Input, Number Lines
    11. 6.11 Convert Digit Characters to Integer
  17. Chapter 7: Functions
    1. 7.1 About Functions
    2. 7.2 skipLines
    3. 7.3 A Program with a Function
      1. 7.3.1 The Function Header
      2. 7.3.2 How a Function Gets Its Data
    4. 7.4 max
    5. 7.5 Print the Day
    6. 7.6 Highest Common Factor
      1. 7.6.1 Using HCF to Find LCM
    7. 7.7 factorial
      1. 7.7.1 Using Factorial
      2. 7.7.2 Combinations
    8. 7.8 Job Charge
    9. 7.9 Calculate Pay
    10. 7.10 Sum of Exact Divisors
      1. 7.10.1 Classify Numbers
    11. 7.11 Some Character Functions
      1. 7.11.1 Position of a Letter in the Alphabet
    12. 7.12 Fetch the Next Integer
  18. Chapter 8: Arrays
    1. 8.1 Simple vs Array Variable
    2. 8.2 Array Declaration
    3. 8.3 Store Values in an Array
      1. 8.3.1 About Not Using Element 0
    4. 8.4 Average and Differences from Average
    5. 8.5 Letter Frequency Count
    6. 8.6 Making Better Use of fopen
    7. 8.7 Array as Argument to a Function
    8. 8.8 String – Array of Characters
      1. 8.8.1 Reverse the Characters in a String
    9. 8.9 Palindrome
      1. 8.9.1 A Better Palindrome Function
    10. 8.10 Array of Strings – Name of Day Revisited
    11. 8.11 A Flexible getString Function
    12. 8.12 A Geography Quiz Program
    13. 8.13 Find the Largest Number
    14. 8.14 Find the Smallest Number
    15. 8.15 A Voting Problem
  19. Chapter 9: Searching, Sorting, and Merging
    1. 9.1 Sequential Search
    2. 9.2 Selection Sort
      1. 9.2.1 Analysis of Selection Sort
    3. 9.3 Insertion Sort
      1. 9.3.1 Analysis of Insertion Sort
      2. 9.3.2 Insert an Element in Place
    4. 9.4 Sort an Array of Strings
      1. 9.4.1 Variable-Length Arrays
    5. 9.5 Sort Parallel Arrays
    6. 9.6 Binary Search
    7. 9.7 Word Frequency Count
    8. 9.8 Merge Sorted Lists
      1. 9.8.1 Implement the Merge
  20. Chapter 10: Structures
    1. 10.1 The Need for Structures
    2. 10.2 How to Declare a Structure
      1. 10.2.1 typedef
    3. 10.3 Array of Structure
    4. 10.4 Search an Array of Structure
    5. 10.5 Sort an Array of Structure
    6. 10.6 Read, Search, and Sort a Structure
    7. 10.7 Nested Structures
    8. 10.8 Work with Fractions
      1. 10.8.1 Manipulate Fractions
    9. 10.9 A Voting Problem
    10. 10.10 Pass Structures to Functions
  21. Index