mag_pi_essentials_learn_to_code_with_c.pdf

(4873 KB) Pobierz
ESSENTIALS
TO
LEARN
CODE
WITH
ON
YOUR
Written by
PROGRAM
WITH THE WORLD’S
MOST POPULAR LANGUAGE
Raspberry Pi
C
Simon Long
SUBSCRIBE TODAY
FROM ONLY £5
Subscriber Benefits
FREE Delivery
Get it fast and for FREE
Exclusive Offers
Great gifts, offers, and discounts
Great Savings
Save up to 35% compared to stores
Rolling Monthly Subscription
Low Monthly Cost
(from £5)
Cancel at any time
Free delivery to your door
Available worldwide
Subscribe for 12 Months
£55
(UK)
£80
(EU)
£90
(USA)
£95
(Rest of World)
Free Pi Zero W Kit with 12 Month upfront subscription only
(no Pi Zero Kit with Rolling Monthly Subscription)
Subscribe online: magpi.cc/subscribe
JOIN FOR 12 MONTHS AND GET A
FREE Pi Zero W
Starter Kit
WITH YOUR SUBSCRIPTION
Subscribe in print
for 12 months today
and you’ll receive:
Pi Zero W
Pi Zero W case
with three covers
USB and HDMI
converter cables
Camera Module
connector
Offer subject to change or
withdrawal at any time
Buy now: magpi.cc/subscribe
SUBSCRIBE
on
app stores
WELCOME
TO
LEARN TO
CODE WITH C
invented in the early 1970s, and
since then has become one of the
most popular and widely used general-
purpose languages. It’s used by a wide range
of programmers, from amateurs working
on simple projects at home, to industry
professionals who write in C for a living. It’s
been used to program everything from the tiny
microcontrollers used in watches and toasters
up to huge software systems - most of Linux
(and Raspbian itself) is written in it. It can
give you control over the smallest details of
how a processor operates, but is still simple to
learn and read. This series is an introduction
to programming in C for absolute beginners;
you don’t need any previous programming
experience, and a Raspberry Pi running
Raspbian is all you need to get started.
Simon Long
T
he C programming language was
FIND US ONLINE
raspberrypi.org/magpi
EDITORIAL
GET IN TOUCH
magpi@raspberrypi.org
DESIGN
Managing Editor:
Russell Barnes
russell@raspberrypi.org
Author:
Simon Long
Sub Editors:
Lorna Lynch and Laura Clay
Critical Media:
criticalmedia.co.uk
Head of Design:
Dougal Matthews
Designers:
Lee Allen, Mike Kay
DISTRIBUTION
THE MAGPI SUBSCRIPTIONS
Select Publisher Services Ltd
PO Box 6337, Bournemouth
BH1 9EH |
+44 (0)1202 586 848
magpi.cc/Subs1
Seymour Distribution Ltd
2 East Poultry Ave, London
EC1A 9PT |
+44 (0)207 429 4000
In print, this product is made using paper
sourced from sustainable forests and
the printer operates an environmental
[
management system which has been
Chapter One
]
assessed as conforming to ISO 14001.
4
This book is published by Raspberry Pi (Trading) Ltd., 30 Station Road, Cambridge, CB1 2JH. The
publisher, editor and contributors accept no responsibility in respect of any omissions or errors
relating to goods, products or services referred to or advertised in this product. Except where
otherwise noted, content in this magazine is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0). ISBN: 978-1-912047-03-1
[ LEARN TO CODE WITH C ]
ESSENTIALS
CONTENTS
06
[
CHAPTER ONE
]
GETTING STARTED
What’s so great about C?
[
SIMON
LONG
]
35
[
CHAPTER SIX
]
FUNCTIONS
Split your code into bite-sized chunks
11
[
CHAPTER TWO
]
VARIABLES & ARITHMETIC
Create variables and do maths
42
[
CHAPTER SEVEN
]
ARRAYS & STRINGS
Handle lists of values, and letters
16
[
CHAPTER THREE
]
CONDITIONS
& COMPARISONS
Control the flow of your C programs
48
[
CHAPTER EIGHT
]
THE STRING LIBRARY
Simplify common operations on strings
22
[
CHAPTER FOUR
]
MORE ADVANCED
FLOW CONTROL
For loops and case statements
55
[
CHAPTER NINE
]
USER INPUT
Reading and interpreting user input
29
[
CHAPTER FIVE
]
POINTERS
Variables have addresses too
62
[
CHAPTER TEN
]
FILE INPUT AND OUTPUT
Learn to read from and write to files
68
[
CHAPTER ELEVEN
]
MORE ABOUT TYPES
AND VARIABLES
Type definitions, enumerations & more
74
[
CHAPTER TWELVE
]
HEADER FILES AND
THE PREPROCESSOR
Splitting code up into multiple files
81
[
CHAPTER THIRTEEN
]
QUICK REFERENCE
Cheat sheets and code examples
Simon Long is a
software engineer
at Raspberry Pi,
with a particular
interest in user
interface design.
He first started
writing C in the
early 90s, and
since then he’s
written software
for a wide variety
of electronic
products, from
mobile phones
to medical
equipment.
Nowadays, he
spends most of
his time fiddling
with the Raspbian
desktop trying
to make it better,
and when he’s
not doing that,
he’s usually
listening to 80s
progressive rock
and solving those
really difficult
crosswords
without any
black squares.
[
Don’t Panic
]
[
Contents
5
Zgłoś jeśli naruszono regulamin