Table Of ContentLearn C Programming
A beginner's guide to learning C programming the easy and
disciplined way
Jeff Szuhay
BIRMINGHAM - MUMBAI
Learn C Programming
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means, without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented.
However, the information contained in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy
of this information.
Commissioning Editor: Richa Tripathi
Acquisition Editor: Denim Pinto
Content Development Editor: Digvijay Bagul
Senior Editor: Rohit Singh
Technical Editor: Pradeep Sahu
Copy Editor: Safis Editing
Project Coordinator: Deeksha Thakkar
Proofreader: Safis Editing
Indexer: Rekha Nair
Production Designer: Joshua Misquitta
First published: June 2020
Production reference: 1260620
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78934-991-7
www.packt.com
To Jaimie, my daughter and "inload manager," this book is for you.
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well
as industry leading tools to help you plan your personal development and advance your
career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.packt.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
customercare@packtpub.com for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
Contributors
About the author
Jeff Szuhay is the principal developer at QuarterTil2, which specializes in graphics-rich
software chronographs for desktop environments. In his software career of over 35 years,
he has engaged in the full range of development activities, from systems analysis and
systems performance tuning to application design, and from initial development through
full testing and final delivery.
Throughout this time, he has taught about computer applications and programming
languages at various educational levels, from elementary school to university level, as well
as developing and presenting professional, on-site training.
I would like to thank, first of all, my teachers who instructed, cajoled, and inspired me.
Notable among these are George Novacky Ph.D. and Alan Rose Ph.D. I would also like to
thank the following colleagues who had the courage to help me see where I went awry:
Dave Kipp, Tim Snyder, Sam Caruso, Mark Dalrymple, Tony McNamara, Jake Penzell,
and Bill Geraci. And lastly, thanks to my wife, Linda, who listened patiently through all of
it.
About the reviewer
B. M. Harwani is the founder of Microchip Computer Education, based in Ajmer, India,
which provides computer literacy in programming and web development to learners of all
ages. He further helps the community by sharing the knowledge and expertise he's gained
over 20 years of teaching by writing books. His recent publications include jQuery Recipes,
published by Apress, Introduction to Python Programming and Developing GUI Applications
with PyQT, published by Cengage Learning, The Android Tablet Developer's Cookbook,
published by Addison-Wesley Professional, UNIX and Shell Programming, published by
Oxford University Press, and Qt5 Python GUI Programming Cookbook and Practical C
Programming, published by Packt.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com
and apply today. We have worked with thousands of developers and tech professionals,
just like you, to help them share their insight with the global tech community. You can
make a general application, apply for a specific hot topic that we are recruiting an author
for, or submit your own idea.
Table of Contents
Preface 1
Section 1: C Fundamentals
Chapter 1: Running Hello, World! 13
Technical requirements 13
Writing your first C program 14
Hello, world! 15
Understanding the program development cycle 16
Edit 16
Compile 18
Many C compilers for every OS 19
A note about IDEs 19
Installing a compiler on Linux, macOS, or Windows 20
Run 21
Verify 22
Repeat 23
A note about debugging 23
Creating, typing, and saving your first C program 24
Compiling your first C program 25
Running your first C program 26
Writing comments to clarify the program later 27
Some guidelines on commenting code 28
Adding comments to the Hello, world! program 30
Learning to experiment with code 31
Summary 32
Chapter 2: Understanding Program Structure 34
Technical requirements 35
Introducing statements and blocks 35
Experimenting with statements and blocks 37
Understanding delimiters 39
Understanding whitespace 41
Introducing statements 44
Introducing functions 46
Understanding function definitions 47
Exploring function identifiers 48
Exploring the function block 49
Exploring function return values 50
Passing in values with function parameters 52
Table of Contents
Order of execution 57
Understanding function declarations 59
Summary 62
Chapter 3: Working with Basic Data Types 63
Technical requirements 64
Understanding data types 64
Bytes and chunks of data 67
Representing whole numbers 69
Representing positive and negative whole numbers 70
Specifying different sizes of integers 70
Representing numbers with decimals 72
Representing single characters 72
Representing Boolean true/false 73
Understanding the sizes of data types 74
The sizeof() operator 74
Ranges of values 76
Summary 78
Chapter 4: Using Variables and Assignment 79
Technical requirements 80
Understanding types and values 80
Introducing variables 81
Naming variables 82
Introducing explicit types of variables 83
Using explicit typing with initialization 83
Exploring constants 84
Literal constants 85
Defined values 87
Explicitly typed constants 88
Naming constant variables 89
Using types and assignment 89
Using explicit assignment, the simplest statement 90
Assigning values by passing function parameters 90
Assignment by the function return value 92
Summary 93
Chapter 5: Exploring Operators and Expressions 95
Technical requirements 96
Expressions and operations 96
Introducing operations on numbers 99
Considering the special issues resulting from operations on numbers 102
Understanding NaN 103
Understanding underflow NaN 103
Understanding overflow NaN 103
Considering precision 104
[ ii ]
Table of Contents
Exploring type conversion 104
Understanding implicit type conversion and values 104
Using explicit type conversion – casting 108
Introducing operations on characters 109
Exploring logical and relational operators 111
Bitwise operators 115
The conditional operator 116
The sequence operator 117
Compound assignment operators 118
Multiple assignments in a single expression 119
Incremental operators 120
Postfix versus prefix incrementation 120
Order of operations and grouping 122
Summary 124
Chapter 6: Exploring Conditional Program Flow 125
Technical requirements 126
Understanding conditional expressions 126
Introducing the if()… else… complex statement 127
Using a switch()… complex statement 131
Introducing multiple if()… statements 135
Using nested if()… else… statements 139
The dangling else… problem 140
Summary 143
Chapter 7: Exploring Loops and Iteration 144
Technical requirements 144
Understanding repetition 145
Understanding brute-force repetition 147
Introducing the while()… statement 150
Introducing the for()… statement 152
Introducing the do … while() statement 155
Understanding loop equivalency 157
Understanding unconditional branching – the dos and (mostly)
don'ts of goto 159
Further controlling loops with break and continue 163
Understanding infinite loops 167
Summary 168
Chapter 8: Creating and Using Enumerations 169
Technical requirements 169
Introducing enumerations 170
Defining enumerations 171
Using enumerations 173
[ iii ]