News for hackers. Stuff that matters.
Re:Modern C, Second Edition by guerrilla on Wednesday September 18, 2019 @02:50PM (#21008642)
The parent says it is "cosmetic", not technical. The parent, being someone who wrote C daily for 25 years, knows what the syntax allows and that it isn't necessary. A constant pointer to a constant pointer to a constant integer should be written "int const * const * const x" because there's only one other way, "const int * const * const x," which is inconsistent, ugly and potentially confusing.
Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @03:25PM (#21009020)
It's bad cosmetic. The predominant style is "const int", following the English adjective before noun.
Re:Modern C, Second Edition by kccqzy on Wednesday September 18, 2019 @04:13PM (#21009593)
It depends on the perspective. To me and in the codebases I've worked in, the predominant style is "int const" instead of the other way around.
Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @09:59PM (#21012506)
My perspective is that "const type" order consistently appears in all versions of the ISO C standard and POSIX.

Every major system API I can remember working with that didn't hide const behind a typedef (like Win32's LPCSTR and whatnot) has it const first.

Speaking of Microsoft, though the API's hide const with a typedef, the MSDN samples are predominantly "const char" order.

I've never seen a man page with "int const".

Searching all of the man pages I have installed on an Ubuntu 18 system here (literally grepping through all of /usr/share/man), I found just one with "char const" or "int const". Namely this one:

  $ man guestfs-hacking
This contains:

        int
        commandvf (char **stdoutput, char **stderror, unsigned flags,
                   char const *const *argv)
In the same man page, there are other declarations that have the predominant convention:

        int
        commandrf (char **stdoutput, char **stderror, unsigned flags,
                   const char *name, ...)
You just worked on some unusual code bases, and are glossing over the language and system API's those code bases depended on, and their respective docs.
Re:Modern C, Second Edition by braindeath on Wednesday September 18, 2019 @04:14PM (#21009607)
C declarators cannot in general and do not follow English grammar rules, so that is a non-sequitur.
Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @04:46PM (#21009992)
const int isn't a declarator; it's a list of declaration specifiers.

Though these can be reordered, it's clear they were intended to follow English.

In a compiler implementation, it's troublesome to enforce the order compared to processing a simple list and setting/checking flags in some data structure, diagnosing invalid duplicates or mutually exclusive situations.

Re:Modern C, Second Edition by braindeath on Wednesday September 18, 2019 @06:24PM (#21011011)
I think you're inadvertently just proving the point.

I was speaking of declarators, not declaration specifiers. In C, a pointer type is part of the declarator and not the specifiers. Therefore, the CV qualifier is also part of the declarator except for the one exception of the specifier. Regardless of whether specifiers were intended to follow English (and this claim is dubious), declarators certainly were not, since it's often more natural to read them right to left, and it's generally most natural to read the left most type-specifier last.

example: char *x[10]; Translated to english this is naturally stated as an array of 10 pointers to char. Not char something something array.

Edit: later on I see you've moved the goal posts about the "uselessness" of cv qualifiers on pointers. I don't know what to tell you, I think that's bullshit, and there are a number of viable use-cases for them regardless of whether you have encountered them. I'm just addressing the consistency part here.

Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @09:03PM (#21012198)
Indeed, declarators must have their postfix chain read first, left to right, then the prefix portions right to left. (Separately at each level of parenthesization, if any, working inside out.)

There is no choice about where to put const inside a declarator; the position affects the nesting level which alters the semantics. So there is no debate to be had there.

Note that in declarators, the const is necessarily to the left of the thing it qualifies, so why would we put it to the right of int:

   int ** const ptr;
                ^^^ this is what is const

   int * const *ptr;
               ^^^^ this is what is const

   const int **ptr;
         ^^^ ^^^^^ these two are const
In the canonical specifier order, nothing to the left of const is tainted by const:

   extern const int **ptr;
                ^^^ ^^^^^^  const and const
   ^^^^^^ unrelated to const
For the best possible consistency, imagine const to be pissing, while the wind is blowing left to right.
Re:Modern C, Second Edition by braindeath on Wednesday September 18, 2019 @09:48PM (#21012450)
Wait what??

Your third example could just as easily and correctly be:

  int const **ptr;
            ^^^^^ this is what is const
True, it is a special case when there is more than one item in the declarator list as to how const applied, but that by itself is secondary to where const goes.

Const qualifies the object. Whether the const is applying to the "int" in some way or is an independent type specifier that applies to each declarator is pretty philosophical. I say it's the latter. The fact is, the very allowance of a cv qualifier in the specifier list introduces an inconsistency or imbalance.

Ultimately this is one of the ultimate bikeshedding argument. There's no right answer, and appeals to spoken language aren't particularly compelling when bikeshedding C of all languages. This isn't AppleScript.

I'm not even terribly interested in this particular point. I'm more interested in quashing the notions that bikeshedded opinions of language syntax have some divine providence. It's just bullshit.

Re:Modern C, Second Edition by kazinator on Thursday September 19, 2019 @02:27AM (#21013820)
We're then missing that the const qualification is applying to the int type.

Also, declarations can have multiple declarators.

   int const *p, volatile **q[3];  // syntax error! not how it works
This isn't bike-shedding. The shed has already been painted. There is a right answer which is not to make your code look weird just because the standard allows it.

No "int const", no i[array] instead of array[i], no deceptive trompe d'oeil nonsense like:

   int* p, q;
You will not solve any issue in C programming by swapping around declarators.

When we write code, stuff that looks weird should signal a bug. When bug-free code looks weird, that is a distracting false positive that grabs my attention for no reason. Don't add gratuitous weird.

Re:Modern C, Second Edition by guerrilla on Wednesday September 18, 2019 @06:24PM (#21011016)
They literally cannot follow English, so I guess they weren't intended to. The order isn't arbitrary for all elements of the list, only the left-most.
Re:Modern C, Second Edition by dooglius on Wednesday September 18, 2019 @05:11PM (#21010264)
I generally think of "int" as also an adjective, with both "const" and "int" describing the variable.
Re:Modern C, Second Edition by iamnotacrook on Thursday September 19, 2019 @07:39AM (#21015259)
"I generally think of "int" as also an adjective, with both "const" and "int" describing the variable."

Eh? int is a noun, surely? How does int describe an int?

I can imagine cost and long being adjectives for the noun int.

Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @05:48PM (#21010608)
That's fair, and double is obviously an adjective. struct foo maybe not so much.
Re:Modern C, Second Edition by guerrilla on Wednesday September 18, 2019 @04:14PM (#21009603)
Your response is an instance of the naturalistic fallacy.
Re:Modern C, Second Edition by kazinator on Wednesday September 18, 2019 @04:40PM (#21009922)
Sure, except for the part where C is a language that uses English or English-based keywords, designed by native English speakers.
Re:Modern C, Second Edition by guerrilla on Wednesday September 18, 2019 @06:17PM (#21010933)
There's no "except" because one cannot conclude how people should write programs from how they do write programs, regardless of circumstances. In any case, languages and their use change. C is 47 years old. Consistent expression structures is more important than analogies to English.

Further, C is not like English and even if it were, one could not conclude that it should be or should continue to be. Moreover, that it was designed by English speakers is not evidence that it is like English or should be like English: most assembly languages were also desiged by English speakers.

Re:Modern C, Second Edition by CawCawCaw on Saturday September 21, 2019 @05:29AM (#21034346)
So ... anything goes, then?
Re:Modern C, Second Edition by bauerd on Wednesday September 18, 2019 @04:52PM (#21010057)
It's not about moral goodness, it's about developer ergonomics. I don't see why the fallacy would apply to all value judgments
Re:Modern C, Second Edition by guerrilla on Wednesday September 18, 2019 @06:23PM (#21011002)
It's isomorphic, but you're right, I used an overly specific term. In any case, one can't conclude that something should be the way it is from the fact that it is the way it is without fruther premises, regardless of the kind of value judgements involved.
Re:Modern C, Second Edition by bauerd on Thursday September 19, 2019 @10:08AM (#21016295)
Implicit key premise seems to be that the more akin a formal (programming) language to it's users' natural one, the more ergonomic it is. I would not necessarily agree, but not read it as ought-from-is.