Smaller C

Book description

For makers looking to use the smallest microcontrollers or to wring the highest performance out of larger ones, the C language is still the best option. This practical book provides a solid grounding in C basics for anyone who tinkers with programming microcontrollers. You'll explore the many ways C enables developers and makers to get big results out of tiny devices.

Author Marc Loy shows you how to write clean, maintainable C code from scratch. This language and its cousin, C++, are still widely used to write low-level code for device drivers or operating systems. By understanding C syntax and its quirks, you'll gain an enduring computer language literacy that will help you pick up new languages and styles more easily.

  • Learn C fundamentals, such as data types, flow control, and functions
  • Explore memory management including how programs work on small devices
  • Understand answers provided in online forums such as Reddit or Stack Overflow
  • Write efficient, custom C code that's both readable and maintainable
  • Analyze the performance of your code and weigh optimizations
  • Evaluate third-party libraries for use in your own projects
  • Create your own libraries to share with others

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. How to Use This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. The ABCs of C
    1. Strengths and Weaknesses
    2. Getting Started
      1. Tools Required
      2. Creating a C “Hello, World”
      3. Compiling Your Code
      4. Running Your Code
    3. Next Steps
  3. 2. Storing and Stating
    1. Statements in C
      1. Statement Separators
      2. Statement Flow
    2. Variables and Types
      1. Getting User Input
      2. Strings and Characters
      3. Numbers
      4. Variable Names
      5. Variable Assignments
    3. printf() and scanf()
      1. printf() Formats
      2. Tailored Output
      3. scanf() and Parsing Inputs
    4. Operators and Expressions
      1. Arithmetic Operators
      2. Order of Operations
      3. Type Casting
    5. Next Steps
  4. 3. Flow of Control
    1. Boolean Values
      1. Comparison Operators
      2. Logical Operators
    2. Branching
      1. The if Statement
      2. The switch Statement
    3. The Ternary Operator and Conditional Assignment
    4. Loop Statements
      1. The for Statement
      2. The while Statement
      3. The do/while Variation
    5. Nesting
      1. Nested Loops and Tables
      2. Variable Scope
    6. Exercises
    7. Next Steps
  5. 4. Bits and (Many) Bytes
    1. Storing Multiple Things with Arrays
      1. Creating and Manipulating Arrays
      2. Review of Strings
      3. Multidimensional Arrays
      4. Accessing Elements in Multidimensional Arrays
    2. Storing Bits
      1. Binary, Octal, Hexadecimal
      2. Octal and Hexadecimal Literals in C
      3. Input and Output of Octal and Hex Values
      4. Bitwise Operators
    3. Mixing Bits and Bytes
    4. Conversion Answers
    5. Next Steps
  6. 5. Functions
    1. Familiar Functions
    2. Function Flow
    3. Simple Functions
    4. Sending Information to Functions
      1. Passing Simple Types
      2. Passing Strings to Functions
      3. Multiple Types
      4. Exiting a Function
    5. Returning Information
      1. Using Returned Values
      2. Ignoring Returned Values
    6. Nested Calls and Recursion
      1. Recursive Functions
    7. Variable Scope
      1. Global Variables
    8. The main() Function
      1. Return values and main()
      2. Command-Line Arguments and main()
    9. Next Steps
  7. 6. Pointers and References
    1. Addresses in C
      1. The NULL Value and Pointer Errors
      2. Arrays
      3. Local Variables and the Stack
      4. Global Variables and the Heap
    2. Pointer Arithmetic
    3. Array Pointers
    4. Functions and Pointers
    5. Managing Memory with Arrays
      1. Allocating with malloc()
      2. Deallocating with free()
    6. C Structures
      1. Defining Structures
      2. Assigning and Accessing Structure Members
      3. Pointers to Structures
      4. Functions and Structures
    7. Pointer Syntax Recap
    8. Next Steps
  8. 7. Libraries
    1. The C Standard Library
      1. stdio.h
      2. stdlib.h
      3. string.h
      4. math.h
      5. time.h
      6. ctype.h
    2. Putting It Together
      1. Filling In Strings
      2. Finding Our Interest
    3. Finding New Libraries
    4. Next Steps
  9. 8. Real-World C With Arduino
    1. Arduino IDE (Win, Mac, Linux)
      1. Installing on Windows
      2. Installing on macOS
      3. Installing on Linux
    2. Your First Arduino Project
      1. Selecting Your Board
      2. Hello, LED!
      3. An External LED Upgrade
    3. Arduino Libraries
      1. Managing Libraries
      2. Using Arduino Libraries
    4. Arduino Sketches and C++
      1. C++ Objects and Variables
      2. More Practice with Objects
      3. C++ Considerations
      4. Object Homework
    5. Next Steps
  10. 9. Smaller Systems
    1. The Arduino Environment
      1. Special Values
      2. Special Types
      3. “Built-In” Functions
      4. Trying Out the Arduino “Stuff”
    2. Microcontroller I/O
      1. Sensors and Analog Input
      2. The Serial Monitor
      3. Is It Hot in Here?
      4. Segmented Displays
      5. Buttons and Digital Input
      6. Just How Hot Is It?
    3. Memory Management on Arduino
      1. Flash (PROGMEM)
      2. SRAM
      3. EEPROM
      4. Remembering Choices
    4. Interrupts
      1. Interrupt Service Routines
      2. Interrupt-Driven Programming
    5. Exercises
    6. Next Steps
  11. 10. Faster Code
    1. The Setup
    2. Floating-Point Versus Integer Math
      1. Floating-Point Math Alternatives
      2. Integer Math Versus No Math
      3. Lookup Tables
      4. The Project So Far
    3. The Power of Powers of 2
    4. Loop Optimizations
      1. Unrolling for Fun and Profit
      2. Recursion Versus Iteration
    5. String Versus char[]
    6. Our Final Offer
    7. Next Steps
  12. 11. Custom Libraries
    1. Creating Your Own Library
      1. Preprocessor Directives
      2. Preprocessor Macros
      3. Custom Type Definitions
      4. Our Car Project
    2. Multifile Projects
      1. Code (.ino) Files
      2. Header Files
    3. Importing Custom Libraries
      1. Facilitating Communication
      2. Retrofitting Our Car
      3. Creating a Controller
      4. Creating the Library
      5. Updating the Car Project
      6. Getting It Under Control
      7. Go Driving!
      8. Documentation and Distribution
    4. Next Steps
  13. 12. Next Next Steps
    1. Intermediate and Advanced Topics
      1. IoT and Arduino
      2. Arduino Source Code
    2. Other Microcontrollers
    3. Industry C/C++
    4. Back to the Future
  14. A. Hardware and Software
    1. Getting the Code
    2. Getting the Hardware: Adafruit
    3. VS Code
    4. Arduino IDE
    5. Fritzing
    6. GNU Compiler Collection
  15. B. printf() Format Specifier Details
    1. Specifier Syntax
      1. Specifier Types
      2. Specifier Flags
      3. Width and Precision
    2. Common Formats
  16. Index

Product information

  • Title: Smaller C
  • Author(s): Marc Loy
  • Release date: May 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098100285