Skip to content

Instantly share code, notes, and snippets.

@weakish
Last active August 24, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weakish/f83b57c89a22cfc0d6ca3a32dd79efa1 to your computer and use it in GitHub Desktop.
Save weakish/f83b57c89a22cfc0d6ca3a32dd79efa1 to your computer and use it in GitHub Desktop.
notes on 21st Century #C

Notes on 21st Century C

Doxygen and CWEB (pp. 43-46)

Doxygen is old-fashioned and hard to install. And I hardly encountered any project using CWEB.

On the other side, Sphinx has a C domain.

Call C from Python (pp. 93-94)

The FFI way is much cleaner.

Pointer arithmetic

p. 112 introduces a trick based on pointer arithmetic:

#include <stdio.h>

int main(){
    char *list[] = {"first", "second", "third", NULL};
    for (char **p=list; *p != NULL; p++){
        printf("%s\n", p[0]);
    }
}

I dislike this cleverness. I prefer a more explicit index variable.

Don't bother explicitly returning from main (p. 116)

I prefer explicitly return 0 from main:

  1. I like explicitness.
  2. It is balanced with other non zero/failure returning.
  3. Do not bother remember another special rule of main.

Cast less (p. 119)

The author prefers (2+0.0) over (dobule) 2.

I think here (double) is much clearer and much more straight than +0.0.

Enums and strings (p.121)

The author prefers short string (for example single character) to global all-caps enums.

I dislike global all-caps enums, too. But I also dislike using short string. If I have to choose one, I would prefer global enums (not necessary all-caps).

Let me review the author's opinion:

I don't want to type these messes, and I use them so infrequently that I have to look them up every time

You do not have to. The IDE/editor will complete them for you.

all caps like yelling

You can ues CamelCase.

We sometimes need to combine flags.

In theses cases, I prefer neither enum nor string. Instead, I prefer separated functions, like readAndAppendLines and readAndOverwriteLines.

On the plus side of enums, you have a small, fixed set of symbols, so if you mistype one, the compiler stops and forces you to fix your typo. With strings, you won't know you had a typo until runtime.

This is the very reason I prefer enum over string.

If the errors were short strings, extension by others would be trivial.

There are not true extension. It is trivial to add those 'extension', but it is hard to distinguish those 'extension' with misuse.

goto

goto, it is generally harmful but is a common present-day idiom for cleaning up in case of different kinds of errors, and it is often cleaner than the alternatives. (p.123)

First goto is restricted with in function, so it is not that evil.

Second, for a language without try-catch, nested function and lambda, there are not many choices.

switch

don't use switch

The alternative to the switch is a simple series of ifs and elses. (p.124)

I dislike the fall-through of switch in C, too. But I still think sometimes switch is more readable than if and else.


License: 0BSD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment