News for hackers. Stuff that matters.
Re:Checked C by jimbob45 on Wednesday December 21, 2022 @02:10PM (#34084975)
Why not just use C++?

Also why can we not get better syntax for C? Function pointer syntax is so bad that people actively avoid using them. #pragma once is a hack. Every string library function is actively dangerous in addition to puts/gets.

I know that nobody wants to be the one to mess with the core language and ruin it but this is awfully low-hanging fruit that hasn’t been fixed in 50 years.

Re:Checked C by WalterBright on Wednesday December 21, 2022 @02:35PM (#34085354)
D has had a BetterC subset of D for a while:

https://dlang.org/spec/betterc.html

Re:Checked C by flohofwoe on Wednesday December 21, 2022 @02:20PM (#34085123)
The stdlib needs a modernization much more than the language, unlike 'C the language', 'C the stdlib' is still stuck deep in the K&R era (together with POSIX).

For a different language syntax it makes a lot more sense to consider using a different language in the first place though, because everybody has a different opinion what such a 'better C' syntax should look like (e.g. it sounds like you'd actually want Zig).

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.

Re:Checked C by mirekrusin on Wednesday December 21, 2022 @02:12PM (#34085017)
It has been fixed. The fix has the codename "zig".
Re:Checked C by seanw444 on Wednesday December 21, 2022 @02:18PM (#34085103)
Looking forward to it becoming more mature. Really cool language.
Re:Checked C by shrimp_emoji on Wednesday December 21, 2022 @02:17PM (#34085090)
I heard it was "Rust".
Re:Checked C by chaosite on Wednesday December 21, 2022 @02:51PM (#34085525)
Rust is more comparable to C++ than C.
Re:Checked C by anfilt on Wednesday December 21, 2022 @03:45PM (#34086172)
Rust is not exactly a C replacement. Definitely the better choice these days over C++ in my opinion if you can use rust.

It can be made to function more like C with #![no_std]. However that is an even less than batteries included experience than C.

Re:Checked C by masklinn on Wednesday December 21, 2022 @03:55PM (#34086269)
> Rust is not exactly a C replacement.

Rust is absolutely a C replacement.

Whether C heads want to use it is a more complicated question. But some are very much into it (e.g. Brian Cantrill).

> It can be made to function more like C with #![no_std]. However that is an even less than batteries included experience than C.

std assumes you have a heap and IO, which most C environments do.

no_std removes all that (leaving only libcore as baseline), however you can re-add a dependency on "alloc" to get allocations and most of the standard collections back (all but hashset and hashmap, because no secure RNG).

You can also / instead fill your needs with third-party crates more dedicated to restricted or freestanding environments, like heapless (https://crates.io/crates/heapless).

Re:Checked C by tialaramex on Wednesday December 21, 2022 @04:31PM (#34086658)
You get a lot more in core (and thus no-std) than the free standing C language.

For example C's qsort lives in the standard library, whereas in Rust although [].sort() needs alloc, the [].sort_unstable() is a core feature!

str::split_once(['5', '7']) will split a string at the first digit 5 or 7... You need considerable work to do that in C, and doing it modifies the string. In Rust it's a core feature.

Of course Rust is relying on the fact that none of the machine code is emitted unless you use these features, indeed split_once is fully generic, the types used only come into existence because you called the function. So presumably this was not practical as the fundamental language for writing Unix fifty years ago.

Re:Checked C by maxbond on Wednesday December 21, 2022 @04:45PM (#34086819)
I think you & GP are using different senses of replacement ("drop in replacement" vs. "replacement for greenfield projects"). You might consider Rust to be the latter (with qualifications, eg, excluding certain architectures & proprietary programming environments) but it's explicitly not the former (it's interoperable with C, but not compatible with C).

I think we should avoid saying "replacement" in general because eliminating all lines of C isn't a goal worth pursuing, C doesn't need to die for Rust to succeed, and there's room enough in this world for a hundred safe systems languages fulfilling different niches. I think a lot of people feel attacked & invalidated by the idea of a "C replacement" that isn't C compatible, like we're telling them their skills are no longer necessary or valuable, which is untrue & leads to flamewars and misunderstandings.

Re:Checked C by zozbot234 on Wednesday December 21, 2022 @05:48PM (#34087412)
Rust is absolutely a "drop-in" replacement to C in most cases even for existing projects, it was absolutely built with that goal in mind. You can go as fine-grained as replacing a single function with a Rust-coded version (even using bindgen to ingest existing C headers) - though that generally involves unsafety because you're dealing with general pointers, extern calls and such. Even Checked C cannot prove these things 100% safe, it just catches common misuse.

(Of course some C projects could abuse the preprocessor in ways that make it infeasible to even replace a C function with a Rust equivalent, but that's incredibly rare. And Rust has macro facilities of its own.)

Re:Checked C by maxbond on Wednesday December 21, 2022 @06:07PM (#34087589)
We're using different senses of "drop in", it's a bit of a vague term. Rust is interoperable with C (it can do C FFI) but it is not compatible with C (C code is not Rust code and Rust code is not C code). Compare this to a language like Carbon or C++; that is what I'd call a "drop in replacement", because they are compatible, using the definitions I've provided. If FFI were sufficient, Python would be a "C replacement."

It's true that there is a path to integrating Rust into C projects and potentially replacing C in that project, I just see lots of threads on HN derailed by the statement "Rust is a C replacement", and I think using more nuanced language would help.

Re:Checked C by zozbot234 on Wednesday December 21, 2022 @06:25PM (#34087739)
By that standard, Carbon is not compatible with C or C++, and neither is Cppfront. One could argue that C-style uses of pinned data or possibly-aliased pointers are not idiomatic in Rust and are a pitfall for interop with the Safe subset; nonetheless these uses can be expressed in the language.
Re:Checked C by Tozen on Wednesday December 21, 2022 @11:50PM (#34089943)
And there are other languages that interop well with and have a FFI for C.
Re:Checked C by xigoi on Thursday December 22, 2022 @11:24AM (#34095046)
C++ is not source-compatible with C and never has been.
Re:Checked C by tmtvl on Wednesday December 21, 2022 @07:12PM (#34088103)
Rust unfortunately has that whole H. R. Giger syntax about it, though.

  things.into_iter().skip(1).each(|wtf| { are_you_kidding(wtf) })?
Compared to a more reasonable language, like, say, Perl:

  map { do_something($ARG) } @things[1..$#things];
Re:Checked C by zozbot234 on Wednesday December 21, 2022 @07:30PM (#34088224)
"Perl" and "more reasonable language" don't belong together. It's a complete oxymoron. The Rust syntax is entirely compositional, other than the || delimiters (which introduce a fresh binding to the subsequent block), and the ? which involves abnormal control flow.
Re:Checked C by masklinn on Thursday December 22, 2022 @04:18AM (#34091380)
I assume GP posted ironically because the Rust snippet is mostly pretty bland function and method calls, the only weird-ish bits are the postfix "?" (which have become very common[0]) and the block syntax[1], while they're using 3 different sigils in a Perl snippet I'm not even sure makes sense (though it's probably valid because perl) e.g. from what little I've retained of perl (against my will) "@ARG" is an array with the command line arguments, $ARG does not exist, this just passes `undef` repeatedly to `do_something`.

[0] https://en.wikipedia.org/wiki/Safe_navigation_operator

[1] e.g. it doesn't have let..else, macros, raw-idents, complicated type signatures, turbofishes, ... which could make the code a lot weirder

Re:Checked C by tmtvl on Thursday December 22, 2022 @06:47AM (#34092427)
$ARG does not exist

If you read the perlvar perldoc (perldoc perlvar or https://perldoc.perl.org/perlvar), you will see it is the English form of $_. You just need to:

  use English;
Re:Checked C by jcranmer on Wednesday December 21, 2022 @06:25PM (#34087746)
> It can be made to function more like C with #![no_std]. However that is an even less than batteries included experience than C.

Rust's #![no_std] mode corresponds to what C calls freestanding mode, and Rust is relatively unusual in many modern languages in actually designing some sort of freestanding mode.

Re:Checked C by pjmlp on Wednesday December 21, 2022 @03:30PM (#34086009)
It isn't a fix when it still allows for use after free.
Re:Checked C by gnramires on Wednesday December 21, 2022 @06:31PM (#34087796)
Check out nim as well, it compiles to c, and it's really an absolute joy to use!
Re:Checked C by Jorengarenar on Wednesday December 21, 2022 @02:57PM (#34085602)
That's why: https://xkcd.com/927

And also: Do you want to see world burning? Because that's how you get the world burning.

Re:Checked C by omeysalvi on Wednesday December 21, 2022 @03:05PM (#34085697)
>Why not just use C++?

Nothing good has come to anyone who started an argument with those words.

Re:Checked C by gavinray on Wednesday December 21, 2022 @03:09PM (#34085749)
Am sort of curious what the difference is between this and templated constexpr functions with span<T, N> in C++