Live data from Hacker News

To become a good C programmer (2011)

fabiensanglard.net

81–90 of 106 posts

Re: To become a good C programmer (2011)

#82
post #77
post #35

Earlier quoted context omitted.

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.

> 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.

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)

#83
post #7

Earlier quoted context omitted.

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 should be: for( int count = 0 ; count 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.

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.

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)

#84

The more I learn C the more I hate it. At first it seems simple and easy but reading "Expert C Programming" is reading a laundry list of what's really messed up with the language. 80% of the problems would be solved by some sane syntactic sugar that compiles down to C

Try learning Rust :)

Re: To become a good C programmer (2011)

#85
post #49

Earlier quoted context omitted.

4. Assemble 5. Load into assembly debugger 6. Learn There is "C", the language, which can be relatively simple. Or hopelessly opaque depending on the author. I think of C the language as just a shorthand for assembly. Only because that's how I use it. http://www.lysator.liu.se/c/bwk-tutor.html But then there is "C" in practice: the specifications, the "standard" libraries, preprocessors, Makefiles, autoconf, etc.

But what would qualify as an "asm code generator" if not a compiler? There are other free compilers. Clang comes to mind.

Let us see... in my day, we called an "asm code generator" an assembler... anyone remember MasterSEKA, Devpac, TRASH'M-One and ASM-one?

Re: To become a good C programmer (2011)

#86
post #19

What's a good practical but small enough project you can do with C? Typically if you are learning Ruby or Node, people recommend creating a blog. What's something like that for C?

You could make a small game with SDL, perhaps?

One can make a commercial game title with C and SDL.

Case in point: Star Control 2, a commercial title with a cult following, which had subsequently been open sourced, then modified to use SDL. Still works all the way from Windows through mobile phones to Solaris, thanks to portable C and the SDL library, and is an excellent game to boot. Original ran on DOS and the 3DO gaming console.

Re: To become a good C programmer (2011)

#87
post #79
post #35

Earlier quoted context omitted.

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.

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 :-)

A ZX80, ZX81 or ZX Spectrum.

Re: To become a good C programmer (2011)

#88
post #35

Earlier quoted context omitted.

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.

> 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.

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)

#89
post #74

Earlier quoted context omitted.

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.

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.

For the record: http://www.tuhs.org/cgi-bin/utree.pl?file=V2/c/nc0

Re: To become a good C programmer (2011)

#90
post #85

Earlier quoted context omitted.

But what would qualify as an "asm code generator" if not a compiler? There are other free compilers. Clang comes to mind.

Let us see... in my day, we called an "asm code generator" an assembler... anyone remember MasterSEKA, Devpac, TRASH'M-One and ASM-one?

  1. Program that reads some input (e.g., C language) and generates asm and/or opcodes.
  2. Program that reads asm and/or opcodes and generates binary numbers ("object files").
No. 1 is what I need. No. 2 is what I call an "assembler". Although terminology means less to me than what a program actually does.
Post reply on HN