Live data from Hacker News

Some Obscure C Features

multun.net

41–50 of 147 posts

Re: Some Obscure C Features

#41
post #26

> a[b] is literally equivalent to *(a + b). You can thus write some absolute madness such as 41[yourarray + 1]. Wow. This has to be the best C obscurity that I've ever seen.

Come on, that might not be obscure for a C ninja master like you, but I'm pretty sure many people had no idea ;-)

Notice how the list is sorted from actually obscure to less interesting.

I even took the time to write a pseudo disclaimer above this one :D

Re: Some Obscure C Features

#43
All craziness but then:

>a[b] is literally equivalent to *(a + b).

Is this obscure? I thought that's pretty much the first thing you learn about arrays in C? It's pointers, all the way down.

Re: Some Obscure C Features

#44
post #7

Although calling the preprocessor "functional" is being too pleasant. The C preprocessor was always a text substitution system, so macro as parameter is not that "obscure". Of course, I may have missed something subtle in the example. It's also not clear how to use that preprocessor example.

It is never obscure when you know it. And sure, it was always meant that way. It's just rarely used this way, which is why it's in this list.

Here's what the example yields once preprocessed:

  struct operator{
    int priority;
    const char *value;
  };

  struct operator operator_negate = {
      .priority = 20,
      .value = "!",
  };

  struct operator operator_different = {
      .priority = 70,
      .value = "!=",
  };

  struct operator operator_mod = {
      .priority = 30,
      .value = "%",
  };

Re: Some Obscure C Features

#45
post #43

All craziness but then: >a[b] is literally equivalent to *(a + b). Is this obscure? I thought that's pretty much the first thing you learn about arrays in C? It's pointers, all the way down.

It's syntactic sugar that's more poorly abstracted than most people realize.

Re: Some Obscure C Features

#46
post #43

All craziness but then: >a[b] is literally equivalent to *(a + b). Is this obscure? I thought that's pretty much the first thing you learn about arrays in C? It's pointers, all the way down.

Agreed, and I never made the connection that array[index] could be written as index[array] haha.

Re: Some Obscure C Features

#47
post #9

Here is an obscure c feature: int main() { int a = 8; { int a = 4; /* a is only scoped to this block */ } printf("%d", a); /* prints 8 */ } It is also why C++ is not a strict superset of C

If you want to tell if your compiler is C or C++, run this program: #include int main() { printf("%d\n", sizeof('a')); } It's the second thing C++ mentions in its list of incompatibility with C (the first is "new keywords"). A more obscure difference is this program: int i; int i; int main() { return i; } It's legal C but not legal C++.

You need to use %zu to print values of type size_t, otherwise you get undefined behavior. So in C, don't run that program. :)

Re: Some Obscure C Features

#48
post #27

Earlier quoted context omitted.

int main() { return 4//**/2 ; }

Line comments have been part of the C language for a long, long time (added in C99); so much so that, especially when discussing the subject on the internet, more and more often they predate some of the younger participants.

Personally, I didn't know that C lacked line comments until I ran into a project that compiled with -std=c89 -pedantic.

Re: Some Obscure C Features

#49
post #34

A nice but I guess more of a linker feature is if you declare a function as __weak__, you can check it at runtime for == NULL to determine if the application was built with the function defined.

I would say most interesting linker features are non standard, so outside of the scope of this article :/

Re: Some Obscure C Features

#50
post #32
post #13

The preprocessor trick of passing function macros as parameters is not that obscure. I have seen it used and I've used it myself. It is very useful when you have a list of static "things" that you need to operate on. Say I have a static list of names and I would like to declare some struct type for each name. I also would like to create variables of these structs at some point, and I would always do so for the entire…

I would call that the X macro pattern, but the wiki article doesn't agree that it should pass the `fn` as the argument. Not sure if that's important.. https://en.wikipedia.org/wiki/X_Macro

Maybe at some point macros could not be passed as arguments? I honestly don’t know. Passing it as a parameter avoids all that define/undefine business.
Post reply on HN