Essentials_C_v1.pdf

(4910 KB) Pobierz
ESSENTIALS
TO
LEARN
CODE
WITH
ON
YOUR
Written by
PROGRAM
WITH THE WORLD’S
MOST POPULAR LANGUAGE
Raspberry Pi
C
Simon Long
FREE
PI ZERO W
Subscribe in print for 12
months today and receive:
A free Pi Zero W (the latest model)
Free Pi Zero W case with 3 covers
Free Camera Module connector
Free USB and HDMI converter cables
SUBSCRIBE
TODAY AND
RECEIVE A
Other benefits:
Save up to 25% on the price
Free delivery to your door
Exclusive Pi offers & discounts
Get every issue first
(before stores)
SAVE
UP TO
25
%
2
AN OFFICIAL
AND
FREE
CAMERA MODULE
CONNECTOR AND USB / HDMI
CONVERTER CABLES
PLUS
PI ZERO CASE
WITH 3 COVERS
Pricing:
Get six issues:
£30
(UK)
£45
(EU)
$69
(USA)
£50
(Rest of World)
Subscribe for a year:
£55
(UK)
£80
(EU)
$129
(USA)
£90
(Rest of World)
Get three issues:
£12.99
(UK)
(Direct Debit) | $37.50
(US)
(quarterly)
How to subscribe:
magpi.cc/Subs-2
(UK / ROW)
Call +44(0)1202 586848 (UK/ROW)
imsnews.com/magpi
(USA)
Call 800 428 3003 (USA)
Search ‘The MagPi’
on your app store:
3
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