Live data from Hacker News

Some Obscure C Features

multun.net

21–30 of 147 posts

Re: Some Obscure C Features

#21
post #18
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

Off the top of my head, I can't think of any language that doesn't permit this. See https://en.wikipedia.org/wiki/Variable_shadowing

Your link mentions CoffeeScript as not allowing shadowing.

Re: Some Obscure C Features

#22
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

Blocks are a common feature

Re: Some Obscure C Features

#23
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++.

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

Re: Some Obscure C Features

#24
In numerical analysis, "Hexadecimal float with an exponent" is not an obscure feature, it's a really nice one! If you want to communicate to somebody an exact number that your program has output, you need to either tell them the decimal number to enough digits + the number of digits (i.e., "Float32(0.1)", which is distinct from "Float64(0.1)"), or you can tell them the same number in full precision in binary, in which case the floating-point standard guarantees that that number is exactly correct and does not depend on how you interpret it. It's really nice for testing numerical code, especially with automated reproducible tests. Completely unambiguous, and I wish more languages had that (I saw the feature in Julia first).

Re: Some Obscure C Features

#25
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

... is this a joke? It's called lexical scoping, and it's supported by more languages than not.

Re: Some Obscure C Features

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

Re: Some Obscure C Features

#27

Earlier quoted context omitted.

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

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.

Re: Some Obscure C Features

#28
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++.

sizeof ('a') doesn't reliably tell you whether you're compiling C or C++. It yields the same result in an implementation where sizeof (int) == 1 (which requires CHAR_BIT >= 16). (The difference is that character constants are of type int in C, and of type char in C++.)

So if sizeof ('a') == 1, then either you're compiling as C++ or you're compiling as C under a rather odd implementation.

Both POSIX and Windows require CHAR_BIT==8. The only systems I know of with CHAR_BIT>8 are for digital signal processors (DSPs).

If you want to tell whether you're compiling C or C++:

    #ifdef __cplusplus
    ...
    #else
    ...
    #fi

Re: Some Obscure C Features

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

see http://conal.net/blog/posts/the-c-language-is-purely-functio...

old but gold

Post reply on HN