
Essential C (2003) [pdf] - kercker
http://cslibrary.stanford.edu/101/EssentialC.pdf
======
e19293001
I've gone through a lot of exercises[0][1] from this site especially on how to
implement a linked list in C. The time spent from the tutorial was absolutely
worth while. I feel happy and satisfied with my self after solving one problem
at a time.

For those who already know the syntax of C but wanted to learn more, the links
I mentioned is a good exercise and opens up a lot of possibilities on what you
can implement. You'll soon be able to realize the importance of linked list
and why you'll want to use a linked list instead of just plain arrays.

[0] -
[http://cslibrary.stanford.edu/103/LinkedListBasics.pdf](http://cslibrary.stanford.edu/103/LinkedListBasics.pdf)

[1] -
[http://cslibrary.stanford.edu/105/LinkedListProblems.pdf](http://cslibrary.stanford.edu/105/LinkedListProblems.pdf)

~~~
conceit
To the contrary, linked lists are cache killers and hardly used nowadays. Or
so they say on here, if I understand correctly.

~~~
huhtenberg
> hardly used nowadays

[https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=IN...](https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=INIT_LIST_HEAD)

That's unbearable. They should just redo this in JavaScript. That's what
everyone uses nowadays. Or so they say on here, if I understand correctly.

~~~
conceit
I tried to appreciate your response, but I cannot fairly judge the code. For
example, those could be performance uncritical parts of code or remnants of 20
year old code where the cache was less effective than now. Are 2000 search
results a lot in a source of this size?

Cache, as opposed to 20+ years ago, or whenever the pattern was developed, is
indeed a thing that everyone is using just now.

------
mynameisnoone
Don't use this. It's really rusty and teaches bad habits superseded by C99.

Here's a proper reference on C99 and C11:

[http://en.cppreference.com/w/c](http://en.cppreference.com/w/c)

And see also reallocarray.

~~~
dave84
Out of interest can you be more specific about the bad habits it teaches?

~~~
baby
And is there a similar resource for current C standards?

------
partycoder
For many of these examples, there's a debugger called "ddd" that can graph
structures and can be useful.

~~~
stevetrewick
I want to love ddd, really I do, conceptually it is awesome and beautiful. In
practice it is (IME) buggy (almost) to the point of uselessness. It's been a
couple of years since I last used it, but it was last updated around 2009 :-(

~~~
exDM69
ddd isn't my day-to-day debugger but I sometimes break it out when I need to
visualize a data structure or keep track of multiple values when doing some
more difficult debugging tasks.

It's far from perfect but still a useful tool.

~~~
stevetrewick
Oh for sure, I've used it a fair bit, it can be an absolute lifesaver for RE
work or building simple VMs, for instance where you need to grok a structure
in its fullness.

------
eplanit
This is an enjoyable comment section (rare to say). The arguments about the
pronunciation of the char keyword is all part of being C programmers. This is
great. :-)

------
thatsadude
This and "C traps and pitfalls" [1] were my favorites back to the days.

[1]
[http://www.literateprogramming.com/ctraps.pdf](http://www.literateprogramming.com/ctraps.pdf)

------
aninteger
Nice to see strlcpy in there. How do we get it into glibc?

~~~
nwmcsween
strlcpy returns offset of dst + len of src which requires strlen(src) which
isn't optional and sort of sucks imo. Something very similar but that doesn't
do strlen(src) such as
[https://godbolt.org/g/Etpuhz](https://godbolt.org/g/Etpuhz) would be better.

~~~
dimman
Well one could argue that in C a string is not a string unless it's NUL
terminated...

It's a choice they made to make it easy to detect truncation. If you don't
care about the return value of strlcpy you can modify it to skip that part
(which IMO will make it worse) but get rid of traversing src until \0 is
found.

If you know you're working with non-terminated "strings" then just use memcpy
instead.

If you're using proper functions you shouldn't end up with a non NUL-
terminated string in the first place, although there are shortcomings in the
commonly used libraries here unfortunately yes. It's hard to create a function
that solves all problems.

(Side note: IMO The return value of latest OpenBSD version of strlcat (cat not
cpy) is a bit weird when size is smaller than strlen(dst))

~~~
nwmcsween
My issue isn't that src is or isn't NUL terminated (strings in c absolutely
must be NUL terminated) its that strlcpy requires a call to strlen (probably
inlineable with lto) which is going to be a lot slower than not. Also there is
a bug in my 'strmcpy' in the last for loop it should be n && *s

------
Xophmeister
The "C Programming Cleverness and Ego Issues" is a good bit of advice:

> Build programs that do something cool rather than programs which flex the
> language's syntax. Syntax -- who cares?

Otherwise, I'd never heard of/seen the `#pragma once` preprocessor directive.
(Not that I do _that_ much C programming!) How common is it to use this over
include guards?

~~~
exDM69
> How common is it to use this over include guards?

Not very. It works in Microsoft's C compiler but can't be relied on elsewhere.

~~~
Xophmeister
Then why mention it over include guards, which are not discussed. Is Stanford
a Microsoft shop, so to speak?

~~~
exDM69
They might have used MSVC for their exercises on this course. They're not
using some C99 features such as stdint.h, which were not available in MSVC
when this article was written.

------
camperman
'char ASCII character -- at least 8 bits. Pronounced "car".'

Pronounced 'car'? Bollocks. Pronounced char as in charred.

~~~
mwfunk
I've always pronounced it as 'care', not because of how the identifier is
spelled but because it's the first syllable of the word 'character'.

I don't think there's a consensus between car/char/care, though, even among
C's creators. But if there was, I would vote for 'care'.

I always thought SQL was another weird one. When discussing the language
itself, you can say 'sequel' or 'ess queue ell' interchangeably, but when
referring to a database that has "SQL" in its name, there is a DB-specific one
true pronunciation:

* SQL Server: 'sequel server'

* PostgreSQL: 'post gress queue ell'

* SQLite: 'sequel light'

* MySQL: 'my ess queue ell'

There's no complete consensus on any of those- just a preponderance of one, or
an officially endorsed pronunciation in a FAQ someplace.

I've also heard 'my squeal' for MySQL, breaking out of the 'sequel'/'ess queue
ell' duopoly altogether.

~~~
robert_tweed
> because it's the first syllable of the word 'character'.

Except the first syllable of 'character' is neither 'car' nor 'care', unless
you have a really weird accent or are intentionally making fun of the English
upper class.

PS: I always liked "squirrel" for SQL but sadly that one never got much
traction.

~~~
telotortium
What's going here is that most Americans have what's known as the marry-merry-
Mary merger[1]. Virtually all English speakers outside of North America, as
well as some Americans, mostly on the East Coast, pronounce the vowels in
these three words differently -- marry is pronounced /mæri/ (/æ/ is the vowel
in "cat"), merry is /mɛri/ (/ɛ/ is the vowel in "pet"), and Mary is /meri/
(/e/ is the vowel in "bait"). In those dialects, any vowel can proceed an /r/
that's intervocalic (i.e., both preceded and followed by another vowel), so
/r/ acts like any other consonant in that sense.

However, in most American dialects, marry-merry-Mary are all merged and
pronounced identically (IPA: /mɛɹi/, though the exact vowel isn't important
here, and all three vowels generally sound the same to merged speakers).
Furthermore, the first syllable of all these words is usually pronounced the
same as the vowel in mare (/mɛɹ/).

So let's put this together. The word "care" is pronounced /kɛr/ in both merged
and unmerged dialects (or /kɛː/ if you don't pronounce the /r/ there). In
unmerged dialects, the word "character" is pronounced /kærəktər/, so it
doesn't make sense that you'd truncate it to /kær/, because /ær/ (or /æː/) at
the end of a syllable doesn't occur in any English dialect[2]. In merged
dialects, however, "character" is pronounced /kɛrəktər/, so it makes sense to
truncate it to /kɛr/, which is a homophone of "care".

[1]: [https://en.wikipedia.org/wiki/English-
language_vowel_changes...](https://en.wikipedia.org/wiki/English-
language_vowel_changes_before_historic_/r/#Mary.E2.80.93marry.E2.80.93merry_merger)

[2]: Except Irish English, see
[https://news.ycombinator.com/item?id=11673067](https://news.ycombinator.com/item?id=11673067).

[3]: I speak a merged dialect, and pronounce "char" as /kɛr/ when I'm reading
C code.

~~~
dave2000
Your post looks interesting and might well be 100% correct but I don't know -
nor do I know anyone who knows - what the various little pronunciation guides
or whatever they're called mean. It's why people tend to say "car as in
carpet".

~~~
cbd1984
It's IPA. It's the closest thing we have to an actual way to write down how
people pronounce things which has any chance at all of being useful.

This "'foo' as in 'bar'" stuff is worse than useless most of the time, as
you've just been shown.

~~~
dave2000
No, "car as in character" is still. Car as in carpet, char as in chart -
simple and doesn't require a course in IPA, which may I remind you precisely
nobody understands.

~~~
cbd1984
> a course in IPA, which may I remind you precisely nobody understands.

Plenty of people understand IPA.

> No, "car as in character" is still.

It's still completely incomprehensible unless you know which vowel sound is
meant, which is the whole point of this sub-thread.

~~~
dave2000
I don't know what happened to my post but i meant "car as in character is
stupid/ambiguous". But it's perfectly possible to understand "car as in
carpet" and it's generally possible to find a similar pronunciation of a
sound.

"Plenty of people" is misleading. Plenty as in thousands, millions of people
worldwide, possibly, not not plenty as a percentage of the english people
world. I'd be surprised if more than 5% of english speakers look at an IPA
description of a sound and do anything other than go "what the fuck does that
mean?".

------
cbd1984
This predates the stdint.h header, which gives you int32_t and so on.

~~~
_kst_
Sort of. The copyright is 1996-2003. <stdint.h> was introduced in the 1999 ISO
C standard.

But as of 2003, support for C99 wasn't very widespread, so omitting <stdint.h>
was understandable.

