Learn C in our brand new MagPi Essentials book!

Rob from The MagPi here again! As I’m sure you’ve noticed, Python is the preferred programming language around these parts. It’s powerful, it’s easy to read, and it’s excellent for teaching coding in general. It isn’t the only language in the world though. We’re happy to reveal that our latest Essentials book will help you learn one of these other languages: C.

4718_essentials-08-code-with-c-cover

Our handy guide to learning C, out now!

If you’ve read the magazine for a while, you’ll have seen pieces on Processing and Node-RED in there. We’ve also been running an ongoing series teaching you C. If getting your learning fix in monthly instalments is too slow for you, though, then Learn to Code with C from the author of that series, Raspberry Pi’s Simon Long, should be just the ticket.

It’s a substantial book, packed with solid, non-gimmicky information. You’ll learn the basics of using C: working with variables, using loops, creating functions and arrays, having user input, controlling your code flow, and much more. You should come away from the book bursting with top-notch knowledge, ready to hack the planet.

Learn to Code with C is available right this instant from our app (which has recently had an overhaul); you can find it on Google Play and the App Store. You can also get the free PDF as usual. I know we’ve been saying that the new range of books will be available in print soon for a while now, but this time we mean really soon. Keep an eye out for future updates.

Any thoughts on the book to close us out, Captain?

37 comments

Avatar

On page 13, the code snippet “c = a + b” is missing, it just goes straight to explaining “This line adds a to b, and stores the result in c”

Rob Zwetsloot

Hi Ryan,

The code snippet is there for us, looks like this: http://imgur.com/a/DJqyX

Does yours look different?

Avatar

what size to print suitable for reading, A4 ?

Avatar

If you wait a few weeks, you’ll be able to buy it already printed…

Avatar

Love these guides – can’t wait til there are more in print – will try and get a set for my classroom!

Avatar

I’ve written a Tetris-program for Raspberry Pi that is beyond this book because it uses function pointers and longjmp. https://github.com/mobluse/blockis

I think the book should include function pointers and longjmp, because it’s what’s needed to emulate polymorphism and exceptions from C++ in C.

Avatar

This book is for beginners – there are pointers to resources for more advanced users at the end.

Avatar

pun intended?

It seems to be a well written little book, tries quite hard to diffuse the exasperation everyone suffers with *&.->

Avatar

I know a lot of professional C programmers. I think that, without exception, they go and look up the syntax for function pointers whenever they find themselves using one.

As Liz quite correctly points out – this is a book for beginners. There are several parts of the C language which it ignores completely, on the basis that beginners do not need to know them. (Frankly, in many cases, even advanced programmers don’t need to know them…)

Avatar

Well maybe not _all_

Some of us (I’m thinking of Dom) can write a definition for a pointer to an array of function pointers.

Gordon

Avatar

Dom has also memorised the Scrabble dictionary. Dom is not a good example.

Avatar

I now have flashbacks to my interview at Broadcom (by one Dr E Upton no less) and that lovely list of “what do each of these variable definitions denote, and how big is each one?”. Except for the likes of Dom, it could almost be considered a cruel and unusual method of torture :-)
Pointer to an array of 10 function pointers, where the function takes a pointer to an array? Size 4 bytes? Pass the dried frog pills please.

Avatar

‘Looking up the syntax of function pointers every time’ – that would be me. More often it’s a pointer to a method once I’m inside a class and want to kill conditional behaviour, and that is always a head scratch.

Mind you I have been known to have to look up the order of the parameters in fwrite in my dumber moments …

Avatar

Sorry MOB, but going to have to tell you off for using longjmp. :)
You don’t need to use longjmp, it is the work of evil. I’ve been a software developer since the 80’s. I’ve used most languages but the majority of my work is in C/C++. I have never had to use longjmp to emulated polymorphism.

Anyone reading this, please erase this function from your memory.

Nothing personal MOB, just want to eradicate some bad practices that have cropped up in c/c++.

Avatar

It’s function pointers that are used to emulate polymorphism. Longjmp is used to emulate exceptions, but you can do it simpler in special cases than this example: http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html

Avatar

This will be a great addition to my “c” library. Thank you!

Avatar

Is there anywhere that you know of in Australia that Magpie books/magazines are sold?

Avatar

I was looking at the MagPi page thinking, these are really cool books for a classroom to have on hand. Especially at that price… Then I thought… BOX SET!!! :-)
Any chance for a spiral bound box set for classroom use? Just an idea… Maybe laminate the pages for durability.

Avatar

First of all, kudos for the amount of effort and creativity the raspberry py foundation has put into bringing programming to the masses.

Very nice introductory book. Simple and down to the essentials… you really do not want to scare people away with pointer arithmetics… :P

However, I would have liked to see a chapter or two, at the end of the book, on how to employ C to program raspberry pi to interact with sensors, peripherals and the like.

Avatar

This is excellent, great work!
If anyone wants to do some graphics / games programming in C on the Pi (or other platforms), I’ve got just the library: https://github.com/simple2d/simple2d
Would be fun to collaborate on teaching, educational resources if folks are interested.

Avatar

This is just me, but I’d prefer to just using the base, which is SDL, and write my own routines on top. Is there a good resource tutorial for using SDL 2 in C? I don’t use C++, much preferring the simplicity of C without any classes. It’s very frustrating that C standard does not come with well defined standard for game development. I’m still stuck with inkey$ and plot point equivalent in C.

Simon mentioned that CLI C programs are niche. I’m wondering if the need to press enter key for every input, even if it’s just a character, is a great contributor of that.

So, SDL and Terminal dev tutorial will be greatly appreciated.

Avatar

Looks like a finally found my excuse to start learning how to code.

Avatar

Nice book, but shouldn’t main return int?

Avatar

See chapter 9…

Avatar

OK, I read chapter 9.

I agree that the argument form is a bit scary for beginners but wouldn’t the alternative correct form “int main(void)” be a better choice than the incorrect “void main(void)?”

Also, having pointed out that it is incorrect, why continue to use it for the rest of the book?

Avatar

I’ve been writing C professionally for 25 years. I cannot honestly recall ever finding it useful to return a value from main – it’s irrelevant in embedded systems, in GUI environments, in standalone programs. As I point out, it’s only really useful if you are calling C programs from a shell script on Unix systems, and that is a fairly niche usage of C these days. As no C compiler I’ve used in that time objects to the use of void as the return type from main, I prefer to save myself the effort of typing a completely redundant “return 0” at the end of every program I write!

Avatar

Shouldn’t an introduction to the C programming language be based on the language specification rather than on “C as Simon does it”? Since we’re talking about using GCC on Linux (i.e. not an embedded system) the specification describes a number of acceptable forms for main and none of them include void as a return type.

Most compilers will ignore a missing return statement, so using ‘int main(void)’ doesn’t necessarily commit you to adding a ‘redundant’ return value, and unlike using a void return type, omitting the return statement doesn’t contradict the spec – it’s even permitted in more recent versions of the C standard.

While I accept some ‘inaccuracy’ is necessary to provide a more gentle introduction to what can be an overwhelming topic, surely it’s better to omit a minor detail, to be filled in later, than present something incorrect that must eventually be unlearned?

Avatar

Fundamentally, this book *is* “C as Simon does it” – if you want a definitive specification of C, there are any number of alternative books available. I’m introducing the language to absolute beginners, and explaining a feature that the specification says they ought to use in spite of it actually offering no useful benefit in the vast majority of situations is only going to confuse people.

As far as I am concerned (and speaking as someone who has written an awful lot of C in his life…), the ability to return a value from a C program is an anachronism. It was useful when people were writing bits of the Unix command-line system in C, but since then, in 99% of cases, it’s a feature which isn’t used. (On Windows, for example, you can’t even access the return value without grovelling about inside environment variables.)

If people do go on to write C programs which require a return value, then I suspect they will need to learn a lot more about C than is in this book. I don’t think anyone doing so will struggle too much with “unlearning” the idea that main doesn’t need to return anything…

Avatar

Congrats to the great work!

This is one of the best resources for beginners I’ve ever seen. It’s not only for Raspberry Pi users, it’s great for anybody who wants to learn C.

Even it’s free of all the burdens scaring people away from learning C it contains everything needed to fully understand the syntax of the language and its concept.

Avatar

“the ability to return a value from a C program is an anachronism.”

Wow! I have nothing more to say.

Avatar

:)

Avatar

chapter 4 : Leaving a loop early
break; statement is never reached,
because a++; is before the if (a==5)
init a to 4 is needed or move a++; after the if statement.

chapter 10 : “Writing a file” may have an error in line
for (value = 30; value < 40; value++)
to get the correct output i had to use
for (value = 48; value < 58; value++)
to get 0123456789 in output.txt (hex 30 = 48 ascii)

Avatar

Aaargh! Yes, you’re quite right in both cases – we’ll get those fixed. I did think I’d run every code block to check it before we went to press, but I missed those two, which were of course the ones with errors in! Many thanks for spotting them, and my apologies for slipping up there.

Avatar

Greetings. I get a warning message from the compiler doing example on page 31. Apparently %d is generating the warning when formatting pointer address. Using %p will compile without error but it prints out a hex number. The screenshot on p33 doesn’t show any problems compiling. Im using Mate on my rpi3. Could this be the issue?

Avatar

Something in your Mate system is setting a higher warning level for gcc than the default in Raspbian – what the compiler warns you about is controlled by a series of environment variables, and Mate is presumably setting different values than Raspbian.

Strictly speaking, you shouldn’t use “%d” for an address, as an address is usually a much larger number than “%d” is meant to display – “%ld” or “%p” (as you discovered) are more appropriate; hence the warning – but given the variation in the sizes of ints on various systems, I opted to just show something that would indicate that the address value was the same in both cases.

It’s another harmless simplification which avoids the need to explain a bunch of other stuff – the point of the example is to show that &variable is the same as the value of a pointer to that variable.

Avatar

I will test with Raspbian. Thanks for your reply and for the book. It’s a good simple intro. I will add complexcity as my projects get more ambitious.
Im really hoping for an Assembly essential book as well as a Java. Sure, I could find tutorials out there, but I like the Magpi’s fun approach.
The pi changed my life…

Leave a Comment

Comments are closed