News for hackers. Stuff that matters.
Re:Checked C by constantcrying on Wednesday December 21, 2022 @02:17PM (#34085089)
>Also why can we not get better syntax for C?

Backwards compatability.

>Function pointer syntax is so bad that people actively avoid using them.

That is because C syntax mistakenly put the return value before the parameters. You use function pointers if you have to.

>#pragma once is a hack.

It is a hack to prevent the previous hack of #ifdef'ing the thing.

>but this is awfully low-hanging fruit that hasn’t been fixed in 50 years.

It is literally impossible to fix.

Re:Checked C by elcritch on Wednesday December 21, 2022 @02:53PM (#34085552)
Ugh, the C syntax makes it so hard to parse, humans or machine.

I recently submitted a PR for the `c2nim` import tool to handle double function pointers. I didn't actually understand the C code until I got the transpiler working despite cdecl.org:

    // posix signal
    void(*signal(int, void (*)(int)))(int);
in Nim becomes:

    var signal*: proc (a1: cint; a2: proc (a1: cint)): proc (a1: cint)

Well I'm 99.5% certain at least. Even now I'm uncertain of the C syntax. And I've not been bold enough to test 3rd order C function pointers. I figure that's probably C code you don't wanna touch if possible.

https://github.com/nim-lang/c2nim/blob/11f2c5363dfe7e8c7c8ce...

The other annoying one is that "signed" and "unsigned" are basically adjectives, but "long" can be both a type and a modifier. So it's difficult to parse unless you're the target C compiler. Technically you can, but you have to use backtracking.

Re:Checked C by constantcrying on Wednesday December 21, 2022 @03:17PM (#34085846)
>Ugh, the C syntax makes it so hard to parse, humans or machine.

As a generalization I completely disagree. But for function pointers specifically, yes, they are easily the worst part of the C syntax.

>void(signal(int, void ()(int)))(int);

That's just declaring signal as function (int, pointer to function (int) returning void) returning pointer to function (int) returning void, DUH. Thank you cdecl.org

But notice how absolutely ugly it is that the final return type comes first, that is due to the C function signatures.

Re:Checked C by slavik81 on Wednesday December 21, 2022 @03:08PM (#34085739)
The signed and unsigned keywords work the same way as long, e.g., `unsigned` alone is equivalent to `unsigned int`.
Re:Checked C by elcritch on Wednesday December 21, 2022 @03:27PM (#34085967)
Yah but you can't have "unsigned unsigned". You can and do have "long long", or possibly a "long long long" in addition to the saner "long int".
Re:Checked C by constantcrying on Wednesday December 21, 2022 @03:37PM (#34086072)
>Yah but you can't have "unsigned unsigned".

Seems like a good thing to be honest.

Re:Checked C by elcritch on Wednesday December 21, 2022 @03:52PM (#34086231)
Lets not give the C committee ideas ;)

But actually I'd suggest the opposite of disallowing "long long" and making it like signed/unsiged as purely a modifier.

Re:Checked C by int_19h on Friday December 23, 2022 @06:24PM (#34111845)
What would you use for your 64-bit integer type, then?
Re:Checked C by masklinn on Saturday December 24, 2022 @01:19AM (#34114505)
int64_t

Long long is not even a 64 bit integer type, it’s a int_least64_t. Could be 128 or 256 bits for all you know.

Re:Checked C by int_19h on Saturday December 24, 2022 @07:17PM (#34122661)
Yeah, it's just the next type in the ladder. But you still need one to map to int_whatever_t.
Re:Checked C by secondcoming on Wednesday December 21, 2022 @03:07PM (#34085723)
That nim version isn’t much better IMO, but I am unfamiliar with the language.
Re:Checked C by kelnos on Wednesday December 21, 2022 @03:29PM (#34085994)
I'm completely unfamiliar with the language but was able to read it from left-to-right very easily:

    var signal*: proc (a1: cint; a2: proc (a1: cint)): proc (a1: cint)
That reads to me as:

Variable signal is a pointer to a function that that takes two arguments (an int, and a function that takes an int), and returns a function that takes one argument (an int).

Of course, I'm assuming my interpretation is correct... perhaps it's not ;)

Re:Checked C by elcritch on Wednesday December 21, 2022 @03:41PM (#34086128)
That's right. It's actually just an ugly bit of code though it's pragmatic.

Note that Nim uses "*" as a public symbol sigil not a pointer. Its a bit odd at first coming from C but is nice and consistent.

Re:Checked C by shirleyquirk on Wednesday December 21, 2022 @04:53PM (#34086882)
I agree, I prefer Nim's alternate syntax for this sort of stuff:

    var signal*: (cint,(cint)->void) -> (cint)->void
Re:Checked C by xormapmap on Wednesday December 21, 2022 @03:54PM (#34086252)
> void(signal(int, void ()(int)))(int);

This is trivial to parse if you use spiral rule, humans or machine.

Re:Checked C by int_19h on Friday December 23, 2022 @06:25PM (#34111853)
The spiral rule is itself inherently non-trivial in application.
Re:Checked C by WalterBright on Wednesday December 21, 2022 @05:17PM (#34087136)
> difficult to parse

Not really. Just create a bit mask.

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cp...

Re:Checked C by elcritch on Wednesday December 21, 2022 @05:49PM (#34087418)
Thanks! I'd read you had a full c compiler, this is awesome.

Looks like there's only about ~388 lines of logic to the bitmask? haha your definition of difficult might not match mine. ;)

Re:Checked C by jcranmer on Wednesday December 21, 2022 @06:14PM (#34087649)
The way you read a declaration in C is imagine you're evaluating an expression. So the example you give:

  void(*signal(int, void (*)(int)))(int)
The result of the expression is void.

Parentheses go first in order of operations, so we evaluate this first:

  (*signal(int, void (*)(int)))
Function call binds before dereference, so this goes first:

  signal(int, void (*)(int))
"signal" becomes the actual declared name. At the bottom, we have a function that takes two parameters, an int, and a function pointer that takes an int and returns void. Now we work outwards.

This function returns a pointer, that when dereferenced, can be called with an int, and returns void when doing so.

In layman's terms, we have a function "signal" that takes an int and a function pointer and returns a function pointer, and both function pointers are to a function that takes an int and returns void.

Is it easy? If you can read C expressions, it can be easy. But it is definitely tedious, and does not come out quickly--it violates the principle that you have <type> <variable name>; it is not what I would consider readable or good design. And it would be easier if types were consistently read in one direction (like Rust types).

Re:Checked C by jimbob45 on Wednesday December 21, 2022 @03:11PM (#34085769)
There are ways to fix backwards compatibility, no? You introduce the new syntax, let it sit for several years, and then deprecate the old syntax. Would it truly be that difficult to introduce a "fn" keyword? Also #includeHeader "myHeader.h" would solve the #pragma once problem.

I simply can't tell if we're not fixing this stuff out of laziness, out of fear, or because no one wants to admit that no one knows who's holding us back.

Re:Checked C by constantcrying on Wednesday December 21, 2022 @03:28PM (#34085976)
C++ addresses most of your concerns already. The headers are C++23 iirc, somewhat similar to your proposal.

I don't think you really understand what C actually is. C basically does not compete as a programming language. On a UNIX like it is the language of the kernel and as such its quirks inevitably represent the quirks of the kernel, you use C if you want to talk to the kernel.

>Would it truly be that difficult to introduce a "fn" keyword?

Yes, literally impossible. Just look at the controversy of C23 removing the almost entirely unused K&R function declaration syntax.