ebook img

Fundamental C: Getting Closer to the Machine PDF

267 Pages·2019·1.665 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Fundamental C: Getting Closer to the Machine

Fundamental C: Getting Closer To The Machine First Edition Harry Fairhead I/O Press I Programmer Library Copyright © 2019 IO Press All rights reserved. This book or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review. Harry Fairhead Fundamental C: Getting Closer To The Machine. 1st Edition ISBN Paperback: 978-1871962604 First Printing, 2019 Revision 1 Published by IO Press www.iopress.info In association with I Programmer www.i-programmer.info The publisher recognizes and respects all marks used by companies and manufacturers as a means to distinguish their products. All brand names and product names mentioned in this book are trade marks or service marks of their respective companies and our omission of trade marks is not an attempt to infringe on the property of others. 2 Preface C is a classic language and it is still an important language. This is because it is closer to the machine than other languages, but it still has enough abstraction for you to create programs that are more or less portable. Perhaps more important, it means you don’t have to learn a new assembly language every time you switch hardware. So learn C and gain the advantage of its speed, simplicity and stay close to the metal. This approach is essential if you are going to create programs for the IoT or embedded processors, but it is also relevant to any program that makes direct use of hardware. Most books on C treat it as if it was just a slightly different take on Java, or some other high-level language. C isn’t Java with limited facilities. It is designed to do a very different job from most modern languages. The key to understanding C is not to just understand the language, what the specifications say, but how this relates to the hardware. C is only a logically constructed language when seen in terms of how it relates to the hardware. For this reason it needs its own approach. But what is that approach? Low-level code works with the underlying representations of the data at the level of bits. In languages such as Java, Python and so on you don’t often question how the data is stored or represented, but the creation of low-level C code does just this. You need to know about addresses, pointers and how things are represented using just binary. Dealing with such topics also brings us into contact with the scourge of the C world – undefined behavior. This cannot be ignored, as it is in so many other books on C, and here not only is it acknowledged, it is explained together with ways to avoid it. C is a simple language and lends itself to being introduced in simple ways, but the simple constructs of C can be put together in ways that produce something complex or at least unexpected. I have tried to indicate how the simple parts of C are combined to make it more powerful than you might expect and how to avoid going too far and making it so obscure that it is difficult to understand. In this book the emphasis is on standard C. If you want to know more about using C on POSIX-compliant and Linux-based systems, this is covered in its companion volume, Applying C For The IoT With Linux ISBN: 978-1871962611. Harry Fairhead March 2019 3 This book is a revised and updated version of the series of articles on the I Programmer website: www.i-programmer.info To keep informed about forthcoming titles visit the I/O Press website: www.iopress.info. This is also where you will also find any errata and update information. You can also provide feedback to help improve future editions. 4 Table of Contents Chapter 1 11 About C Why C?...............................................................................................11 Standards and C................................................................................13 Platform Dependent or Independent – Undefined Behavior...........14 Safe Coding........................................................................................16 Summary...........................................................................................17 Chapter 2 19 Getting Started NetBeans and GCC............................................................................19 NetBeans C/C++ Under Linux and Windows.................................20 Installing GCC...................................................................................21 A First Program.................................................................................22 Using Tool Collections......................................................................24 Remote C/C++ Build Servers...........................................................25 Creating a Hello World Program.......................................................29 From the Command Line..................................................................30 Summary...........................................................................................31 Chapter 3 33 Control Structures and Data Variables, assignment and expressions............................................33 Program Structure.............................................................................35 Comments..........................................................................................36 The Flow of Control..........................................................................37 The Conditional................................................................................40 The Compound Statement................................................................41 Loops..................................................................................................42 Flow of Control in Practice...............................................................52 Fractional Loops – Break and Continue...........................................55 Nesting...............................................................................................56 The Switch Statement.......................................................................57 Summary...........................................................................................61 5 Chapter 4 63 Variables Memory Basics..................................................................................64 The Numeric Data Types..................................................................65 The char Type...................................................................................66 Working With Boolean values..........................................................67 Floating Point....................................................................................67 Complex Numbers.............................................................................68 Declaring Variables...........................................................................68 Initialization......................................................................................69 Literals...............................................................................................71 Implementing Variables....................................................................72 Exact Size Variables..........................................................................73 Type Modifiers..................................................................................74 Variable Lifetimes.............................................................................75 lvalue and rvalue...............................................................................76 Summary...........................................................................................77 Chapter 5 79 Arithmetic and Representation Positive Integers................................................................................79 Hexadecimal......................................................................................80 Unsigned Integer Arithmetic............................................................80 Negative Numbers.............................................................................81 Two’s-Complement Overflow...........................................................83 Representations and printf...............................................................85 Summary...........................................................................................88 Chapter 6 89 Operators and Expression Evaluating Expressions.....................................................................89 Operators in C...................................................................................91 Expressions, Type and Casting.........................................................94 Side Effects........................................................................................99 Postfix and Prefix Increment............................................................99 Conditional and Comma.................................................................100 Sequence Points and Lazy Evaluation............................................101 Summary.........................................................................................104 6 Chapter 7 105 Functions, Scope and Lifetime What is a Function..........................................................................105 Why Functions?..............................................................................106 Parameters.......................................................................................107 Return Values..................................................................................108 The main Function..........................................................................110 Declaring Functions........................................................................110 The Expression Principle - Pass-by-Value.....................................111 Inline Functions..............................................................................112 Local Variables – Lifetime and Scope............................................113 Block Scope.....................................................................................114 Global Variables – Program and File Scope...................................116 Heap and Stack................................................................................118 Summary........................................................................................121 Chapter 8 123 Arrays The Array.........................................................................................123 Arrays and For Loops......................................................................125 Assigning Arrays - Pointers............................................................126 Multi-Dimensional Arrays..............................................................128 Arrays as Parameters.......................................................................130 Arrays and Return...........................................................................131 The Problem of Size........................................................................133 Enumerations..................................................................................135 Summary.........................................................................................138 Chapter 9 139 Strings Strings..............................................................................................139 String Handling Functions..............................................................141 Buffer Overflow...............................................................................144 Convert to String - sprintf...............................................................145 Input – Buffer Problems..................................................................146 Low-level I/O...................................................................................148 A Safe Way To Do Input – String Conversion...............................149 Summary.........................................................................................152 7 Chapter 10 153 Pointers Declaring a Pointer – The Dereference Operator...........................153 Initializing a Pointer – The Address Operator...............................155 Pointers and Arrays.........................................................................157 Pointer Arithmetic...........................................................................158 Functions and Pointers...................................................................160 Allocating Memory..........................................................................163 Casting Pointers...............................................................................165 Type Punning..................................................................................166 Type Punning and Undefined Behavior.........................................169 Returning an Array from a Function..............................................171 Is int* a Type?.................................................................................171 Pointers to Functions......................................................................173 Pointers to Pointers.........................................................................174 Immutability....................................................................................177 Summary.........................................................................................181 Chapter 11 183 Structs The Basic Struct..............................................................................183 Value Semantics..............................................................................185 Struct Parameters and Return.........................................................187 Pointers to Structs...........................................................................188 Using typedef...................................................................................190 Nested Structs.................................................................................193 Padding............................................................................................194 Union...............................................................................................196 Bit Fields..........................................................................................199 Summary.........................................................................................202 Chapter 12 203 Bit Manipulation The Bitwise Operators.....................................................................203 Signed v Unsigned..........................................................................204 Masks...............................................................................................204 Using Masks....................................................................................207 Shifting Values................................................................................207 Rotate...............................................................................................210 Testing a Bit.....................................................................................211 Endianism........................................................................................212 Some Examples...............................................................................213 Summary.........................................................................................217 8 Chapter 13 219 Files The File Idea....................................................................................219 Basic Files........................................................................................219 Text Mode........................................................................................221 Binary Files......................................................................................222 Structs as Records...........................................................................224 Buffering..........................................................................................225 Character I/O...................................................................................225 Positioning Functions.....................................................................226 End Of File Errors............................................................................227 Random Access...............................................................................227 File Operations................................................................................229 Sharing Files – Locking...................................................................230 Summary.........................................................................................231 Chapter 14 233 Compiling C – Preprocessor, Compiler, Linker Four Stages of Compilation.............................................................233 The Preprocessor - Include.............................................................235 Macros.............................................................................................236 Conditional Compilation................................................................240 Linking.............................................................................................242 A Static Library...............................................................................243 Creating a Dynamic Library............................................................246 Make................................................................................................247 Conclusion.......................................................................................250 Summary.........................................................................................251 9

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.