top | item 11671985

Essential C (2003) [pdf]

228 points| kercker | 10 years ago |cslibrary.stanford.edu | reply

89 comments

order
[+] e19293001|10 years ago|reply
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

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

[+] DonaldFisk|10 years ago|reply
My favourite language is Lisp, in which I use "linked" lists all the time, but I never use them in C. Use of lists without a garbage collector and either dynamic typing (as in Lisp) or strong static typing (as in Haskell) is a pain, and C has none of these built in. This means that, in C, you need to allocate memory every time you add something to your list, and hold the pointer to it in order to free it later. You also need to keep track of which types are stored, and where.

If your problem requires a lot of list processing, use a language more suitable for it. Otherwise, use extensible arrays, which are easy to implement in C.

conceit also pointed out that lists also increase the likelihood of cache misses, which slows down the program a little bit. Many C programmers care about that. I don't share their priorities - it is important to me, but less so than correctness, ease of programming (allowing more time for exploration and finding a more efficient algorithm), and response times.

[+] conceit|10 years ago|reply
To the contrary, linked lists are cache killers and hardly used nowadays. Or so they say on here, if I understand correctly.
[+] mynameisnoone|10 years ago|reply
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

And see also reallocarray.

[+] dave84|10 years ago|reply
Out of interest can you be more specific about the bad habits it teaches?
[+] partycoder|10 years ago|reply
For many of these examples, there's a debugger called "ddd" that can graph structures and can be useful.
[+] stevetrewick|10 years ago|reply
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 :-(
[+] eplanit|10 years ago|reply
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. :-)
[+] aninteger|10 years ago|reply
Nice to see strlcpy in there. How do we get it into glibc?
[+] nwmcsween|10 years ago|reply
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 would be better.
[+] _kst_|10 years ago|reply
Even nicer to see that it warns users away from strncpy.
[+] Xophmeister|10 years ago|reply
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|10 years ago|reply
> 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.

[+] camperman|10 years ago|reply
'char ASCII character -- at least 8 bits. Pronounced "car".'

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

[+] mwfunk|10 years ago|reply
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.

[+] mikestew|10 years ago|reply
Bollocks, pronounced char as in "char". I did not know this was even in dispute. Not that i care how anyone else pronounces it, i just thought it was blindingly obvious since we already have an English word spelled exactly the same as what we use in code to declare the thing.
[+] broodbucket|10 years ago|reply
I used to say "car", but it's definitely inferior to "char", since you can refer to char * as "char star" which is a lot of fun to say.
[+] mbreese|10 years ago|reply
And how do you pronounce "varchar"?

I've always pronounced "char" as "car". Dunno why, I learned C from a book, so no one got to tell me I was wrong.

[+] robert_tweed|10 years ago|reply
I too pronounce it that way, since I learned C from books and just read it as it was printed.

I later noticed some people do pronounce it "car" which just seems odd. Why change it, if it doesn't save a syllable? It's not a truncation of how "character" is said either, because that's a different vowel sound.

Saying 'car' causes problems when learning C++, because all OOP examples on composition start "Car has-an Engine".

[+] corndoge|10 years ago|reply
Pronounced char as in care
[+] conceit|10 years ago|reply
as in "carred" you mean? :P
[+] rootbear|10 years ago|reply
Argument over GHif vs Jif in 3, 2, 1...
[+] cbd1984|10 years ago|reply
Pronounced care as in the first syllable of character.
[+] sklogic|10 years ago|reply
Why? It's from "character", you do not want it charred.
[+] cbd1984|10 years ago|reply
This predates the stdint.h header, which gives you int32_t and so on.
[+] _kst_|10 years ago|reply
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.

[+] npx|10 years ago|reply
Yeah, that was my first thought after a cursory reading, y u no <stdint.h>? There seems to be a lot of great content regardless.

I'd love to see an updated version.