Live data from Hacker News

Essential C (2003) [pdf]

cslibrary.stanford.edu

11–20 of 84 posts

Re: Essential C (2003) [pdf]

#11

Earlier quoted context omitted.

Can you give an example where not using them 'inline' makes code harder to understand?

Consider the idiomatic way of interating backward through a array: for(i=n; i-- > 0 ;) { /* operate on a[i] */ } converting i--; to a statement at the start of block makes it less clear that it's part of the iteration idiom rather than a ad hoc adjustment that's specific to this particular logic. There are other examples, but they're either more involved or statementification is less obviously wrong.

Hmm, I think `for (i = n - 1; i >= 0; --i)` is way clearer and maybe more common?

edit: Ah unsigned underflow. :O

Re: Essential C (2003) [pdf]

#12
post #11

Earlier quoted context omitted.

Consider the idiomatic way of interating backward through a array: for(i=n; i-- > 0 ;) { /* operate on a[i] */ } converting i--; to a statement at the start of block makes it less clear that it's part of the iteration idiom rather than a ad hoc adjustment that's specific to this particular logic. There are other examples, but they're either more involved or statementification is less obviously wrong.

Hmm, I think `for (i = n - 1; i >= 0; --i)` is way clearer and maybe more common? edit: Ah unsigned underflow. :O

Yeah, so then you write

    for (size_t i = n-1; i 
It works fine (unsigned overflow is well defined) but it's even less clear.

Re: Essential C (2003) [pdf]

#13
post #11

Earlier quoted context omitted.

Hmm, I think `for (i = n - 1; i >= 0; --i)` is way clearer and maybe more common? edit: Ah unsigned underflow. :O

Yeah, so then you write for (size_t i = n-1; i It works fine (unsigned overflow is well defined) but it's even less clear.

It seems sensible to always just use signed values for indices. Indices are difference types, which should include negative values so that you can subtract two indices and get a sane delta. The range of signed values seems 'big enough.'

Re: Essential C (2003) [pdf]

#17

I highly recommend the CS50 course to get familiar with C: https://www.youtube.com/playlist?list=PLhQjrBD2T381L3iZyDTxR... Sure it doesn't get in details about the language but you get the essential and the videos are great.

It's how I started my career 7 years ago. Amazing course and lecturer

Re: Essential C (2003) [pdf]

#18
post #9

This is a pretty neat guide if you’re cheap and have moral qualms about pirating K&R. Still, I think the best introduction to C remains K&R.

K&R is woefully out of date. Gives you no info on how to do things safely and sanely. And encourages a leet style of programming that results in catastrophic edge case bugs. As you can see in comments above where naive code that iterates backwards through an array fails when the array size is 0. Worse K&R leet style buys you absolutely nothing with a optimizing compiler written in the last 30 years.

Re: Essential C (2003) [pdf]

#19

Earlier quoted context omitted.

Consider the idiomatic way of interating backward through a array: for(i=n; i-- > 0 ;) { /* operate on a[i] */ } converting i--; to a statement at the start of block makes it less clear that it's part of the iteration idiom rather than a ad hoc adjustment that's specific to this particular logic. There are other examples, but they're either more involved or statementification is less obviously wrong.

That's the idiomatic way? Cool. The more straightforward-looking way, for(i = n-1; i >= 0; i--) { /* operate on a[i] */ } breaks if i is unsigned, like a size_t.

Yep. That why it's a idiom, rather than a obvious-way-of-doing-it-that-anyone-competent-would-use.

Re: Essential C (2003) [pdf]

#20
post #13

Earlier quoted context omitted.

Yeah, so then you write for (size_t i = n-1; i It works fine (unsigned overflow is well defined) but it's even less clear.

It seems sensible to always just use signed values for indices. Indices are difference types, which should include negative values so that you can subtract two indices and get a sane delta. The range of signed values seems 'big enough.'

> Indices are difference types

Umm, no? Indices are ordinals[0], forming the canonical/nominal well-ordering of a collection such as a array.

> an ordinal number, or ordinal, is one generalization of the concept of a natural number that is used to describe a way to arrange a (possibly infinite) collection of objects in order, one after another. [...] Ordinal numbers are thus the "labels" needed to arrange collections of objects in order.

0: https://en.wikipedia.org/wiki/Ordinal_number

Post reply on HN