News for hackers. Stuff that matters.
Re:To become a good C programmer (2011) by scarmig on Sunday May 01, 2016 @11:58AM (#11606662)
K&R is often recommended, and it's certainly fun to read and accessible. But I've also heard it's outdated, and doesn't rally focus much on modern C software design, mostly because the world knew little about it when K&R was written.

Thoughts?

Re:To become a good C programmer (2011) by ams6110 on Sunday May 01, 2016 @02:14PM (#11607207)
Some of the examples in K&R are not up to date with respect to modern security practices. They are almost case studies in how to implement code vulnerable to buffer overflows, etc.

Or maybe it's better to say -- sanity checking on inputs is an (unmentioned) exercise left to the reader.

Re:To become a good C programmer (2011) by bitwize on Sunday May 01, 2016 @12:35PM (#11606806)
K&R Second Ed. was published going on two decades after C first emerged. Already many versions of Unix and the applications to run on them were written at the time of K&R Second. You can hardly say that little was known about software design with C at the time it came out.

It won't cover C99, C11, or modern practices to mitigate buffer overflows. But it is still an excellent introduction to the fundamentals of C. It's also aimed at people who already know how to program in, say, Pascal, so is probably best not approached as a rank beginner.

Re:To become a good C programmer (2011) by simplicio on Sunday May 01, 2016 @12:32PM (#11606798)
I first learned C from K&R, but I'm not really that big of a fan. I find the style of inter-mixing new information in the middle of examples kind of hard to follow. I prefer to have the basics set out in front, and than get a bunch of examples demonstrating them. I'm sure people with different learning styles have an easier time with it, though.
Re:To become a good C programmer (2011) by satysin on Sunday May 01, 2016 @12:05PM (#11606695)
K&R is still a great book but there are also other great books. Programming in C by Kochan and A Modern Approach by King are two fantastic books and much more suited to a true beginner than K&R is IMHO. Also Head First C and 21st Century C (second edition) are great books to read.

The exercises in K&R are superb though and I highly recommend taking the time to do them all while you read K&R which I still feel you should, it is a small book so shouldn't take long to read.

Re:To become a good C programmer (2011) by ktRolster on Sunday May 01, 2016 @04:06PM (#11607652)
Since we're throwing out books, "The Absolute Beginner's Guide to C" (by Perry) has helped a number of people who felt completely overwhelmed by programming in general...
Re:To become a good C programmer (2011) by robertelder on Sunday May 01, 2016 @12:05PM (#11606689)
It is still relevant due to the fact that K&R C is still extremely (but not completely) forward compatible with even the most modern c++ standards. Many software design best practices have changed since then, but if you read a more modern book some of the first principles of the C language will have to be either repeated or omitted by a modern book.
Re:To become a good C programmer (2011) by signa11 on Sunday May 01, 2016 @12:48PM (#11606852)
> thoughts ?

well, there is a 'ModernC' book by Jens Gustedt of INRIA available here: http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf

which might suit your fancy ?

Re:To become a good C programmer (2011) by dsfuoi on Sunday May 01, 2016 @12:15PM (#11606733)
I think the style should be avoided; code shouldn't be compact and variable names should be descriptive.

Artificial example:

    for( int i = 0 ; i < c+j ; i += b )
should be:

    for( int count = 0 ; count < sum+extra ; count += skip )
It may be allowed to use i in place of count here, but this is the only place where single name variables should be permitted and only if i is really just a simple array index iterator.
Re:To become a good C programmer (2011) by jimjimjim on Sunday May 01, 2016 @03:57PM (#11607623)
There should always be a balance between brevity and being too verbose.

all single letters = bad, java style thisIsALoopCounter = bad.

and in the c world i is almost universally understood to be used as a loop counter and index. If I came across 'count' in a c code I (briefly) might think it was related to a count of items in an array or similar.

Re:To become a good C programmer (2011) by dsfuoi on Monday May 02, 2016 @05:47AM (#11610226)
I agree. The problem is that those single character names get abused and you end up with code that consists only of them, including the function arguments, which is the worst.
Re:To become a good C programmer (2011) by GregBuchholz on Monday May 02, 2016 @12:12AM (#11609159)
I hope you don't have a stroke:

https://github.com/jsoftware/jsource/blob/master/jsrc/cip.c

...just one of the many source files that make the interpreter for the J language.

Re:To become a good C programmer (2011) by dsfuoi on Monday May 02, 2016 @02:15AM (#11609471)
The 'style' even leaked out to file naming: a.c, ab.c, af.c, am.c, c.c, ca.c, cc.c, cd.c, cf.c, ...

Brilliant!

I doubt that this is either serious (unobfuscated) or handwritten code though.

Re:To become a good C programmer (2011) by GregBuchholz on Monday May 02, 2016 @12:16PM (#11612747)
No, it really is the hand-written source code for the J language (http://jsoftware.com/) interpreter. You may also enjoy:

https://news.ycombinator.com/item?id=2336749

http://code.jsoftware.com/wiki/Essays/Incunabulum

Re:To become a good C programmer (2011) by kps on Monday May 02, 2016 @10:01AM (#11611399)
Re:To become a good C programmer (2011) by tmuir on Sunday May 01, 2016 @03:17PM (#11607471)
Even "i" can be changed to "index". My rule of thumb is this: If you wouldn't use the abbreviation when speaking, don't use it when coding.

I don't understand why 'i' has been given a pass. The argument boils down to saved keystrokes. Typing is not the bottleneck.

Re:To become a good C programmer (2011) by dllthomas on Monday May 02, 2016 @04:29AM (#11609964)
> I don't understand why 'i' has been given a pass.

Because it is well enough established (in both programming and mathematics) that it communicates literally no less information to the reader than "iterator" or "index" would in the same context.

> The argument boils down to saved keystrokes.

Not at all. I, for one, find it easier to see the shape of the whole expression when the variable names are shorter.

Re:To become a good C programmer (2011) by kps on Monday May 02, 2016 @09:59AM (#11611375)
Yes; prolixity yields clarity in programming about as much as in prose. The old quip needs an update: a modern programmer can write COBOL in any language.
Re:To become a good C programmer (2011) by kleiba on Sunday May 01, 2016 @05:30PM (#11607939)
My guess would be that it derives from the mathematical notation for sums.

But apart from that, I think using i,j,k as index variables in for-loop has become such a de-facto standard that I would only prefer long names such as "count" or "index" in situations where multiple indices may cause confusion. But then I'd probably use even more specific names than "count" or "index".

Re:To become a good C programmer (2011) by Gibbon1 on Monday May 02, 2016 @04:20AM (#11609934)
> I don't understand why 'i' has been given a pass.

The reason is that in FORTRAN variables starting with i, j, k..n were defined as integers.

Back in the day I learned fortran first, then basic. People that learned basic first often would use the letter 'a' as a loop index instead of 'i'

Now days I still use 'i' but often use indx instead because my editor doesn't highlight single letter variables easily.

Re:To become a good C programmer (2011) by majewsky on Monday May 02, 2016 @05:19AM (#11610133)
I'm okay with "i" (like others said, it's a well-known shorthand for "iterator" or "index") but I often use "idx" because it can easily be combined with other words in nested loops (e.g. "rowIdx" and "columnIdx").
Re:To become a good C programmer (2011) by dsfuoi on Sunday May 01, 2016 @03:41PM (#11607569)
I agree, and I have calculated this a while ago this based on average number of lines a programmer writes in 8 hours. I don't have the actual numbers but unless you type slower than ~30 words per minute, speed it isn't a bottleneck.

I have been using index instead of i in while loops lately. It's surprisingly concise.

Re:To become a good C programmer (2011) by husted on Monday May 02, 2016 @05:02AM (#11610080)
When I started to program in C I used "n" for the longest time, simple because it was quicker on to type on my first computer; "Next N" was a quick two taps on the N key. Guess the platform :-)
Re:To become a good C programmer (2011) by teddyh on Monday May 02, 2016 @09:08AM (#11610982)
A ZX80, ZX81 or ZX Spectrum.