Live data from Hacker News

Some Obscure C Features

multun.net

101–110 of 147 posts

Re: Some Obscure C Features

#101

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 whic…

Note that you can uniquely identify all floats by printing them with printf(“%1.8e”):

https://randomascii.wordpress.com/2013/02/07/float-precision...

Re: Some Obscure C Features

#103
post #97

No mention of trigraphs? They are one of my favourite obscure C language features that I've never used. Excerpt from GCC man page: Trigraph: ??( ??) ?? ??= ??/ ??' ??! ??- Replacement: [ ] { } # \ ^ | ~ Missing backslash on your keyboard? No problem, just type ??/ instead.

More than once have I written something like this

  if(condition)
    printf("WTF??! value: %d", value);
...only to have the compiler nag me about it. That's pretty much the only situation I've come across trigraphs :)

Re: Some Obscure C Features

#104
post #97

No mention of trigraphs? They are one of my favourite obscure C language features that I've never used. Excerpt from GCC man page: Trigraph: ??( ??) ?? ??= ??/ ??' ??! ??- Replacement: [ ] { } # \ ^ | ~ Missing backslash on your keyboard? No problem, just type ??/ instead.

IIRC these are a legacy of BCPL

Re: Some Obscure C Features

#105

Most of these are due to the cruft added in C99 and later. Compile-time trees are possible without compound literals. More than twenty years ago, I made a hyper-linked help screen system a GUI app whose content was all statically declared C structures with pointers to each other. At file scope, you can make circular structures, thanks to tentative definitions, which can forward-declare the existence of a name, whose…

Compound literals are fun. You can use them to implement labelled and optional parameters to functions too:

    struct args { int a; char *b; };
    #define fn(...) (fn_((struct args){__VA_ARGS__}))

    void fn_ (struct args args) {
        printf ("a = %d, b = %s\n", args.a, args.b);
    }

    fn (0, "test");
    fn (1); // called with b == NULL
    fn (.b = "hello", .a = 2);
(As written this has a subtle catch that fn() passes undefined values, but you can get around that by adding an extra hidden struct field which is always zero).

Re: Some Obscure C Features

#106

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 whic…

Note that you can uniquely identify all floats by printing them with printf(“%1.8e”): https://randomascii.wordpress.com/2013/02/07/float-precision...

As the parent already said, the nice thing about hexadecimal floating-point is that the standard MANDATES that you get exactly the float/double you have uniquely described. The assertion “you can uniquely identify all floats by printing them with printf("%1.8e")” on the other hand relies on the compiler using round-to-nearest for conversions between binary and decimal, which the standard does not mandate and which, even when the compiler documents the choice, is sophisticated enough that the compiler/printf/strtod may get it wrong for difficult inputs:

https://www.exploringbinary.com/tag/bug/

Re: Some Obscure C Features

#107
post #8

sizeof actually doesn't evaluate its expression for side effects most of the time; only if the operand is a variable-length array is it evaluated.

That is not guaranteed either:

"If the size expression of a VLA has side effects, they are guaranteed to be produced except when it is a part of a sizeof expression whose result doesn't depend on it." (from: https://en.cppreference.com/w/c/language/array)

In the example this is not a problem, because int[printf()] means you must call printf() to get the return value and determine the size of the array.

Re: Some Obscure C Features

#108
post #97

No mention of trigraphs? They are one of my favourite obscure C language features that I've never used. Excerpt from GCC man page: Trigraph: ??( ??) ?? ??= ??/ ??' ??! ??- Replacement: [ ] { } # \ ^ | ~ Missing backslash on your keyboard? No problem, just type ??/ instead.

IIRC these are a legacy of BCPL

The story I heard was that the trigraphs were added during standardization because ISO 646, the international version of ASCII, did not require the characters [ \ ] { | }.

Fun fact: ISO 646 is also the reason that IRC allows these characters in nicknames. IRC was created in Finland, and the Finnish national standard had placed the letters Ä Ö Å ä ö å at those code points.

Edit: That doesn't explain the trigraphs for # ^ ~. I'm guessing some EBCDIC variants lacked those. Or some other computer vendor on the committee still supported some other legacy character set.

Re: Some Obscure C Features

#109
post #97

No mention of trigraphs? They are one of my favourite obscure C language features that I've never used. Excerpt from GCC man page: Trigraph: ??( ??) ?? ??= ??/ ??' ??! ??- Replacement: [ ] { } # \ ^ | ~ Missing backslash on your keyboard? No problem, just type ??/ instead.

IIRC these are a legacy of BCPL

They weren't in K&R. I recall a story about a standardization meeting, on the way to ANSI C, where representatives from some European(?) country that didn't have some of the necessary punctuation on their country-specific keyboards, essentially snuck the trigraphs into the spec when the other representatives weren't looking.
Post reply on HN