News for hackers. Stuff that matters.
Re:Using Zig's translate-C to understand weird C code by WithinReason on Wednesday November 03, 2021 @06:32AM (#29092881)
The author translates 1 line of C mess to 7 lines of Zig mess which are then manually simplified through transforms. Why not just apply transforms to the original C code? Or is this only for people who know Zig syntax but not C?
Re:Using Zig's translate-C to understand weird C code by kristoff_it on Wednesday November 03, 2021 @08:48AM (#29093841)
> Or is this only for people who know Zig syntax but not C?

It's a blog post hosted on zig.news (so intended for an audience that knows Zig), I think we can safely assume that it's not meant to be an universal guide for understanding C code, but rather the recount of a (somewhat minor) experience lived by the author. It's even stated upfront:

> @rep_stosq_void on Twitter posted this strange sample of C code, and I wanted to show my process of understanding this contrived C code.

"I was presented with this code, and this is what I did to understand it"

Re:Using Zig's translate-C to understand weird C code by sobeston on Wednesday November 03, 2021 @03:36PM (#29099284)
Author here, I'm just showing how I did it. It took a few minutes to do it for myself - figured it would be worth the 15 minutes or so it'd take to write it down. Just sharing my process, there's nothing too serious about this post.

I've happened to have written a lot of C, but some areas (like this one) I'm not as confident in. This way of casting, and working with types is very poor syntax in my opinion (I mean below in this thread you have people arguing about the spiral rule and such, it's obviously a common confusion).

Zig's way of expressing types and pointers is far superior, these transformations I made were done quickly and I didn't feel like there was any ambiguity or confusion in anything. Just a series of simple reductions, there are no "tricks" or easy mistakes to make in the code. It feels like a trivial proof.

Obviously I am biased, this was posted to zig.news. I thought it was a neat showcase of translate-c, and how zig does some of these things nicer. I'm not telling everyone that they should do what I did, but this works for me and I'm happy to share.

Re:Using Zig's translate-C to understand weird C code by mannykannot on Wednesday November 03, 2021 @08:17AM (#29093571)
As someone unaware of Zig and its translate-C feature, I read the article as being about those things, not C.
Re:Using Zig's translate-C to understand weird C code by rightbyte on Wednesday November 03, 2021 @06:51AM (#29092990)
Or like use the cdecl cli tool ...
[dead]
Re:Using Zig's translate-C to understand weird C code by dnautics on Wednesday November 03, 2021 @01:56PM (#29098151)
You do understand that zig started out by removing things from C and then "adding rich constexprs" and that's it? It's arguably simpler than C, C is actually two languages (C and C preprocessor), three if you add make, five if you add cmake and autoconf.
Re:Using Zig's translate-C to understand weird C code by drfuchs on Wednesday November 03, 2021 @03:02PM (#29098905)
What's the motivation for changing the visually appealing C syntax "ptr->field" to the whack "ptr.*.field"?
Re:Using Zig's translate-C to understand weird C code by sobeston on Wednesday November 03, 2021 @03:19PM (#29099097)
ptr.field works.
Re:Using Zig's translate-C to understand weird C code by 10000truths on Wednesday November 03, 2021 @06:04PM (#29100951)
make, cmake and autoconf are not not part of the C language, they are build tools.
Re:Using Zig's translate-C to understand weird C code by dnautics on Wednesday November 03, 2021 @06:49PM (#29101491)
In practice you can't be productive in a large project in C without make, increasingly cmake (at the very least for reading someone else's c project). And it's part of the zig agenda to unify build tools, too. We are now in the 2nd decade of the 21st century, it is not strange for programming languages to be opinionated about build tools, ruby has rake, rust has cargo, js has, well, js is a mess, don't copy js, elixir has mix, etc (and those are just the languages I know).
Re:Using Zig's translate-C to understand weird C code by 10000truths on Thursday November 04, 2021 @02:59AM (#29104203)
That doesn’t make those tools part of the C standard, though. The standard does not mandate the use of any particular build tool for C projects beyond a conformant C compiler. There is nothing stopping me from using meson, ninja, bazel, a Frankenstein shell script, etc. to build a C project. Hell, there is nothing stopping me from using make to build non-C projects - it was designed as a generic dependency resolution tool.
Re:Using Zig's translate-C to understand weird C code by nicoburns on Thursday November 04, 2021 @06:21AM (#29105319)
> There is nothing stopping me from using meson, ninja, bazel, a Frankenstein shell script, etc. to build a C project

There is if you intend to use libraries as those libraries may well have chosen a build system other than the one you have chosen meaning you'd have to rewrite their build scripts. This is far from the experience of using libraries in languages with unified build systems which is typically as simple as adding a line with the version of the library you want to a manifest file.

Re:Using Zig's translate-C to understand weird C code by dnautics on Thursday November 04, 2021 @10:06AM (#29107342)
> That doesn’t make those tools part of the C standard, though

So? I'm talking about the C experience, not the C standard.

Re:Using Zig's translate-C to understand weird C code by sedatk on Wednesday November 03, 2021 @03:02PM (#29098907)
You somehow managed to criticize Zig for being something that C isn't, lacking macros, preprocessor and whatnot, and yet praise Go and Python for being opinionated about their features, which don't have preprocessors or macros.
Re:Using Zig's translate-C to understand weird C code by abaracadabi on Wednesday November 03, 2021 @03:18PM (#29099079)
Not really. I praise Python for simple syntax and Go for simplicity of language features.

And I criticize Zig for trying to be another kitchen sink (despite having no macros or preprocessor).

Re:Using Zig's translate-C to understand weird C code by AtomToast on Wednesday November 03, 2021 @02:25PM (#29098501)
Having few features that are small and knowable is pretty much the idea behind Zig though. It's supposed to be more of a C replacement than another C++/Rust. If you know C you can just read through the Zig language spec and see how stuff correlates and you just learned zig.

Removing a whole secondary language for macros actually just makes the language simpler

Re:Using Zig's translate-C to understand weird C code by xigoi on Wednesday November 03, 2021 @02:18PM (#29098416)
Since you like Python's syntax, what's your opinion about Nim?
Re:Using Zig's translate-C to understand weird C code by abaracadabi on Wednesday November 03, 2021 @04:06PM (#29099620)
Honestly, I’m not sure about Nim. I’d probably reach for Go before Nim because I hate layers in-between things and too many language paradigms.

Main thing is that all these hip new languages will be dead in a couple years because they don’t do anything all that innovative. Making “another C” or “another C target” versus Go which focuses on channels, slices, and go routines. These other languages that “make a better X” simply will be sidelined in production deployments because no matter the language there will always be warts.

Really folks keep reinventing the wart in the name of something “better”.

Re:Using Zig's translate-C to understand weird C code by hansvm on Wednesday November 03, 2021 @04:23PM (#29099789)
Maybe it's still not the language for you or for your projects, but for anyone else stumbling across this I don't think it's a fair characterization. Would you mind elaborating on where you think I'm off-base in the following?

> head-strong nerds inventing things

Let people have their fun.

> preprocessor allows you to add some logic before compilation

Zig's comptime feature does address that need. For most use cases where comptime and the preprocessor would both suffice, comptime has tons of strong advantages (type checked, errors traced back to the right lines, ability to write arbitrary turing-complete code without unholy syntactic black magic, no double evaluation, no need to wrap everything in extra parentheses, ....). It explicitly does not address use cases where you would actually want a textual preprocessor (like making the language look like Fortran), the argument being that the burden on reading unfamiliar code would be too high. The ease with which you can write arbitrary comptime code also enables you to do things like embed lookup tables in the binary, which in C you would ordinarily do by copy pasting from another tool (or writing constants by hand) or adding yet another preprocessor to the mix.

Comptime isn't a clear win over preprocessing in all cases, but having written a lot of C and a little Zig, if I had to choose one for a hypothetical new project I'd be tempted to use Zig just for access to that one feature.

> Syntax

Zig does feel a little clunky to write to me right now, but I think that's mainly do to how often I'm using @someBuiltin() in Zig when I would be using an operator or an additional language feature in something like Python.

That said, the syntax itself is incredibly simple. You can check that yourself for Zig [0], Python [1], and Go [2]. Zig has far fewer syntactic quirks and abilities than either of the other two. You have functions, operators, code blocks, types, reserved characters/keywords for builtin stuff, a few kinds of literals, and a little bit of syntactic sugar for working with structs.

> Don't reinvent C++...for instance, I even consider C better than Zig just because the features are small and knowable

To each their own. FWIW, Zig is explicitly and actively avoiding becoming a kitchen sink language like Rust or C++. They've added a few new features, and I personally find it easier to keep track of those than of all the different kinds of undefined behavior I might stumble across in C, especially since most of those (defer, errdefer, labeled break, ...) behave exactly as your intuition suggests they would.

[0] https://ziglang.org/documentation/master/#Grammar

[1] https://docs.python.org/3/reference/grammar.html

[2] https://golang.org/ref/spec

Re:Using Zig's translate-C to understand weird C code by dosshell on Wednesday November 03, 2021 @07:54AM (#29093420)
Or like use https://cppinsights.io/ ...
Re:Using Zig's translate-C to understand weird C code by michaelmior on Wednesday November 03, 2021 @08:10AM (#29093531)
That gives the following output which I don't think is much more readable.

  void f(int * a)
  {
    void * p = reinterpret_cast<void *>(&a);
    ***reinterpret_cast<int *(*)[]>(p) = 1;
  }
Re:Using Zig's translate-C to understand weird C code by cies on Wednesday November 03, 2021 @08:17AM (#29093572)
Maybe after several steps of manual processing it is :)

(not sure what steps that should be)

Re:Using Zig's translate-C to understand weird C code by michaelmior on Wednesday November 03, 2021 @11:33AM (#29096090)
Sure but I don't think C++ Insights really helped.