
Beej's Guide to C Programming (2007) - da02
http://beej.us/guide/bgc/
======
da02
Also good as a supplement:

* A Tutorial on Pointers and Arrays in C: [http://home.netcom.com/%7Etjensen/ptr/pointers.htm](http://home.netcom.com/%7Etjensen/ptr/pointers.htm)

* Right-left Rule: [http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html](http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html) (via HN)

* More free C stuff: [https://github.com/EbookFoundation/free-programming-books/bl...](https://github.com/EbookFoundation/free-programming-books/blob/master/free-programming-books.md#c)

(I had to go search through lots of stuff to finally find resources that
explain things in a way that I can understand. Once it is explained to me in a
certain way, C code suddenly feels a lot less cryptic and I feel a lot less
hopeless/useless.)

------
unwind
Very good.

A pet peeve is that it consistently says "sizeof()", like this:

 _You can use the sizeof() operator to determine how many bytes of memory a
certain type uses. (I know, sizeof() looks more like a function than an
operator, but there we are.)_

BUT: sizeof does not in fact need the parentheses always!

Syntactically they are part of the argument, and only needed when the argument
is a type name, i.e. the argument looks like a cast to that type.

So:

    
    
        int x;
        printf("%zu == %zu\n", sizeof x, sizeof (int));
    

It's very annoying to see the guide kind of glance off the way things really
are, into confusion.

Very glad to see it doesn't cast the return value of malloc(), too. :)

~~~
geekodour
About casting the malloc() :
[http://c-faq.com/malloc/mallocnocast.html](http://c-faq.com/malloc/mallocnocast.html)

~~~
unwind
Yeah, that's a good resource. I also recommend
[https://stackoverflow.com/questions/605845/do-i-cast-the-
res...](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-
malloc).

------
nathell
I like the style. It's somewhat reminiscent of _why's Poignant Guide to Ruby.
(I wouldn't be surprised if the inspiration were actually in the reverse
direction.)

~~~
chris_wot
BeeJ has been around for longer than why

~~~
da02
It would have been nice if they could have collaborated together. "Freelance
professors" that could write code and illustrate it w/ words and graphics.

------
kzisme
As someone who currently works with C# - is there much of a point to learning
C? I've always found it interesting, but never had a reason to jump into it.

~~~
Koshkin
While C remains _the_ single most important programming languages of all time,
its share of popularity is shrinking, giving way to C++, C#, Java, and Swift
(from Objective-C).

Nevertheless, I don't think anyone should call themselves a (true) programmer
unless they know C.

C is easy to learn, because it's a small language. If you are not new to
programming, pretty much all there is to know is in the second edition of the
K&R book.

~~~
ZenoArrow
> "Nevertheless, I don't think anyone should call themselves a (true)
> programmer unless they know C."

You could make an even stronger claim about knowing an assembly language. The
point being that understanding how a computing device works is helpful in
getting the best out of it. If that wasn't your point, then C is no more
special than any other popular programming language.

Also, whilst I only know the basics of C, it's pretty clear things have moved
on considerably since the days of K&R. It doesn't cover any of the
enhancements included in C99 and C11, nor does it cover the tooling and coding
conventions (the ones that help avoid problems) that have become commonplace
since 1989.

For a more up-to-date reference, I've heard good things about 21st Century C:

[http://shop.oreilly.com/product/0636920033677.do](http://shop.oreilly.com/product/0636920033677.do)

~~~
Koshkin
> _If that wasn 't your point_

It wasn't; it is perfectly possible and normal to use C as one would any other
programming language, i.e. without knowing much about the computer
architecture. Even the pointers, often seen as something low-level, aren't
really: the rules of their behavior and use are very simple, and they are easy
to describe at a high level.

C is special in that, as I said, it has been, and remains, a very important
programming language in its ubiquity and sheer power.

~~~
ZenoArrow
> "C is special in that, as I said, it has been, and remains, a very important
> programming language in its ubiquity and sheer power."

In that case, you could say the same about Java, Python, C#, etc... If being a
"true" programmer is just about getting the job done, any of those languages
will suffice. C may be a useful tool for certain performance critical code,
but that's a very small fraction of what's required in most day-to-day
programming activities. I would say the following list describes the key
qualities of good code (in the order described, from most to least important,
for most cases):

Correctness, clarity, conciseness, performance.

As for pointers, you can describe them at a high-level but to understand why
they're a low-cost abstraction it's helpful to understand how computers work.
That's the type of knowledge that C can help expose you to.

------
oseph
This is wonderful. I see that this guide is sort of "old news" to many of the
experienced programmers hanging out here. But as someone who isn't that
experienced and about to begin a course on Data Structures + Algorithms using
C, this is amazing. Thank you.

~~~
sn9
These two sites were invaluable for me when I was in your position:
[http://iso-9899.info/wiki/Main_Page](http://iso-9899.info/wiki/Main_Page) and
[http://c-faq.com/](http://c-faq.com/)

------
davidw
Well that's cool - digging around a bit, I found out that he runs a hacker
group right here in Bend: [http://bend.hackersguild.us/#page-
main](http://bend.hackersguild.us/#page-main)

------
nippples
BeeJ's guides don't get as much visibility as they deserve.

------
kumarvvr
Are there any good tutorials for cross platform development with C++ on
windows.

I just want to avoid MSVC compilers and use clang or gcc.

Also, C++ module/package/library management is very very confusing. You have
to configure multiple files in multiple folders and multiple configuration
entries.

I tried it almost a dozen times and just left it out of frustration.

Compared to development with python, it's just mind numbing.

~~~
jcelerier
> Are there any good tutorials for cross platform development with C++ on
> windows.

Install QtCreator with MinGW. Here you can just compile your code and have it
work.

> You have to configure multiple files in multiple folders and multiple
> configuration entries.

not on sane systems where you just do `apt-get install libsdl1.2-dev` or
`pacman -S gtk` or `brew install qt boost`

~~~
bigtunacan
I hear QtCreator mentioned on Hacker News quite frequently. A couple of
questions, if you don't mind.

1) How well does it work if you are trying to stick to C and not go the C++
route?

2) They are GPL licensed, so not very permissive, or quite expensive for a
commercial license. Is there a significant difference in feature set between
the open source & commercial versions for someone that just wants to take it
for a spin?

~~~
jcelerier
> How well does it work if you are trying to stick to C and not go the C++
> route?

The IDE works fine, but you're missing on a lot :p. You can use raw makefiles
or, better, CMake with it; the integration with cmake's server mode in the
latest version is top notch.

> They are GPL licensed, so not very permissive, or quite expensive for a
> commercial license. Is there a significant difference in feature set between
> the open source & commercial versions for someone that just wants to take it
> for a spin?

First, there are two things: Qt and QtCreator. Qt is a C++ library (and also
has its own DSL, QML which is a godsend to make modern UIs) and QtCreator is
an IDE. While it has a lot of Qt-specific features (eg UI designer, some Qt-
specific autocompletions, Qt examples on the front page), it can just be used
as a general-purpose IDE for C, C++, Python, and Nim. As such, its license
does not matter for you as a developer, unless you want to ship a product with
the IDE (for instance Sailfish OS and Ubuntu used a customized QtCreator as
the IDE for their respective SDK).

Then, Qt is as you saw, under multiple licenses: commercial, gpl, and lgpl.

* _all the libraries_ are under GPL.

* most modules are additionnally under LGPL.

* two tools are only under the commercial license: QML compiler (but it has been superceded by another approach that was open-sourced) and pre-made images for some embedded boards (Boot2Qt, basically useful for embedded software that runs fullscreen just on top of the linux kernel with no GUI environment, generally on the raw framebuffers or drivers provided by the board vendor).

The things that are under GPL but not LGPL are here:
[http://doc.qt.io/qt-5/qtmodules.html#gpl-licensed-
addons](http://doc.qt.io/qt-5/qtmodules.html#gpl-licensed-addons) (three
modules: virtual keyboard, charts and data visualization). Everything else on
the page (ie more than enough for "taking it for a spin") can be used from
proprietary apps (you have to make a readme at some point that says that you
use Qt, like any other open source libs.) . The simplest is to just use the Qt
shared libs, but you can also link them statically:

[https://www.gnu.org/licenses/gpl-
faq.html#LGPLStaticVsDynami...](https://www.gnu.org/licenses/gpl-
faq.html#LGPLStaticVsDynamic)

~~~
vram22
>QML which is a godsend to make modern UIs

What advantages does QML provide over using "just the Qt library from C++"?
Faster development? I've used an earlier version of Qt (3 or 4) a bit, but
never tried QML.

~~~
jcelerier
If you are familiar with modern web development, think of Vue.js or React's
reactive bindings but as part of the language syntax. See
[https://qmlbook.github.io/en/ch04/index.html#qml-
syntax](https://qmlbook.github.io/en/ch04/index.html#qml-syntax)

e.g. when in "C++ Qt" you'd do :

    
    
        connect(m_button, SIGNAL(clicked()), m_object, SLOT(on_clicked()));
    

or more recently

    
    
        connect(m_button, &QPushButton::clicked, m_object, &MyObj::on_clicked);
    

or even

    
    
        connect(m_button, &QPushButton::clicked, this, [=] { /* do stuff */ });
    

QML would be :

    
    
        Button {
          onClicked: /* do stuff * /
        }
    

Even better is stuff like

    
    
        connect(m_slider, &QSlider::valueChanged, 
                m_object, [=] (int val) { m_item->setWidth(3 * val) });
        m_object->setWidth(3 * m_item->value());
    

which becomes

    
    
        Slider {
          id: slider
        }
    
        Item {
          width: 3 * slider.value
        }
    
    

It has a slightly stronger type system than JS; not as strong as TypeScript
though. The idea is that when you really need strong typing you do it in C++
and expose your objects to QML (which is a one-liner: every object part of the
Qt meta-type system can be used from the QML's side and have its signals and
slots called there).

Of course, the big drawback is the performance loss that you get from going
from optimizable C++ to Qt's modified V8 engine. However, if you had a
graphics-heavy C++ Qt app that relied on QGraphicsScene / QGraphicsView it
_could_ be faster in QML since there it's all OpenGL / D3D12 (and hopefully
Vulkan) + a modern scene graph, instead of CPU rendering. So like always,
benchmark benchmark benchmark :)

~~~
vram22
Yes, the QML code does seem a lot simpler/shorter. Thanks.

------
throwaway7645
The first listed program to add up all the integers from 1 to the scanned
number wouldn't compile under GCC for me and the error was that "i" undeclared
(first use in this function). I had to declare "i" as an "int" to make it
work. It looks like it accepted the declaration in the top with num and result
or within the for loop.

------
chaoticmass
I remember using Beej's guide to learn how to do TCP/IP stuff in C around ten
years ago. Very well written and easy to follow.

[http://beej.us/guide/bgnet/](http://beej.us/guide/bgnet/)

------
bap
Beej was a couple years ahead of me at Chico - he was an awesome guy then and
it looks like he's been keeping at it ever since.

------
ausjke
Beej wrote one of the best programming content online and I hope he can update
some of them(C11,posix IPC, epoll/poll,etc).

------
ateesdalejr
Yup. This guy is great. Downloaded this a while ago for offline reading while
I'm on the go.

------
yorwba
Beej's Guide to Network programming
([http://beej.us/guide/bgnet/](http://beej.us/guide/bgnet/)) is also pretty
good. It was effectively used as the textbook in my networking class. (There
was also an actual textbook, but almost nobody bothered to read it.)

~~~
TorKlingberg
It's definitely worth reading, but be aware that it's quite old-fashioned in
that it focuses entirely on blocking I/O and select(). Also this page fails to
explain why you would call shutdown():
[http://beej.us/guide/bgnet/output/html/multipage/shutdownman...](http://beej.us/guide/bgnet/output/html/multipage/shutdownman.html)

~~~
nix0n
What has replaced select()?

~~~
elromulous
epoll on linux. Other apis on other oses.

More reading: [http://www.kegel.com/c10k.html](http://www.kegel.com/c10k.html)

~~~
fish_fan
Kqueue on the bsds and darwin.

------
omginternets
BeeJ's guides to other things: [http://beej.us/guide/](http://beej.us/guide/)

~~~
gre
He even wrote a League of Legends guide.

Beej's Guide to Lane Control and the Early Game
[http://forums.na.leagueoflegends.com/board/showthread.php?t=...](http://forums.na.leagueoflegends.com/board/showthread.php?t=149979)

------
alexanderbisc
This is amazing. It's a shirt and a quick understanding guide.

~~~
robotresearcher
That's a unusual combo.

------
kuharich
Prior discussion:
[http://news.ycombinator.com/item?id=5241220](http://news.ycombinator.com/item?id=5241220)

~~~
nikbackm
That's for network programming.

