Re: "A diagram of C23 basic types"

Liste des GroupesRevenir à cl c  
Sujet : Re: "A diagram of C23 basic types"
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 10. Apr 2025, 15:29:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vt8kkp$333f0$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
User-Agent : Mozilla Thunderbird
On 10/04/2025 14:06, David Brown wrote:
On 10/04/2025 13:42, bart wrote:
On 10/04/2025 08:53, David Brown wrote:
On 09/04/2025 21:11, bart wrote:
On 09/04/2025 18:26, BGB wrote:
>
It might have also been interesting if C allowed optional named arguments:
int foo(int x=3, int y=4)
{
   return x+y;
}
>
foo() => 7
foo(.y=2) => 5
>
Likely would be following any fixed arguments (if present), and likely (for sake of implementation sanity) named arguments and varargs being mutually exclusive (alternative being that named arguments precede varargs if both are used).
>
Well, at least ".y=val" as "y: val" likely wouldn't go over well even if it is what several other languages with this feature used (well or, "y=val", which is used in some others).
>
In the most likely case, the named argument form would be transformed into the equivalent fixed argument form at compile time.
   So: "foo(.y=2)" would be functionally equivalent to "foo(3,2)".
>
There are all sorts of problems in adding this to C. For example, this is legal:
>
   void F(int a, float b, char* c);
   void F(int c, float a, char* b);
   void F(int b, float c, char* a) {}
>
The sets of parameter names are all different (and that's in the same file!); which is the official set?
>
C has had flexibility here for all sorts of reasons.  But if named parameters were to be added to the language without significant extra syntax, then this particular issue could be solved in at least two very simple ways.  Either say that named parameter syntax can only be used if all of the function's declarations in the translation unit have consistent naming, or say that the last declaration in scope is the one used.  (My guess would be that the later, with compilers offering warnings about the former.)
>
Of course that lets someone declare "void f(int a, int b);" in one file and "void f(int b, int a);" in a different one - but that does not noticeably change the kind of mixups already available to the undisciplined programmer, and it is completely eliminated by the standard practice of using shared headers for declarations.
>
>
>
Another is to do with defining default values (essential if named arguments are to be fully used). First, similar thing to the above:
>
   void F(int a = x + y);
   void F(int a = DEFAULT);
>
>
Default arguments are most certainly not essential to make named parameters useful.
>
Then the advantage is minimal. They are useful when there are lots of parameters, where only a few are essential, and the rest are various options.
 That is one use-case, yes.  Generally, functions with large numbers of parameters are frowned upon anyway - there are typically better ways to handle such things.
 Where named parameters shine is when you have a few parameters that have the same type.  "void control_leds(bool red, bool green, bool blue);". There are a variety of ways you can make a function like this in a clearer or safer way in C, but it requires a fair amount of extra boilerplate code (to define enum types for simple clarity, or struct types for greater safety).  Named parameters would make such functions safer and clearer in a simple way.
 
>
They /can/ be a nice thing to have, but they are merely icing on the cake.  Still, there is an obvious and C-friendly way to handle this too - the default values must be constant expressions.
>
Well, the most common default value is 0. But do you mean actual literals, or can you use macro or enum names?
 I mean actual constant expressions, as C defines them.  That includes constants (now called "literals" in C23), constant expressions (such as "2 * 10"), enumeration constants, and constexpr constants (in C23). Basically, things that you could use for initialisation of a variable at file scope.
 
>
Because it is those name resolutions that are the problem, not whether the result is a compile-time constant expression.
>
 I don't see that at all.
It probably wouldn't be too much of a problem in C, since outside of a function, there is only one scope anyway. But it can be illustrated like this:
   enum {x=100};
   void F(int a = x);
   int main(void) {
       enum {x=200};
       void F(int a = x);
       F();
   }
What default value would be used for this call, 100 or 200? Or could there actually be two possible defaults for the same function?
Declaring functions inside another is uncommon. But you can do similar things at file scope with #define and #undef.
Or maybe the default value uses names defined in a header, but a different translation unit could use a different header, or it might just have a different expression anyway.
(I would disallow this:
    void F(int a, int b = a)
where the default value for 'b' is the parameter 'a'. That would be ill-defined and awkward to implement, plus you could have parameter defaulting to each other.)

Not really; the above is inside a formal parameter list, where '=' has no special meaning.
 That is exactly the point - "=" has no special meaning inside a function call.
But that wasn't a function call! So you can use '=' in declaration, and perhaps '.' and '=' in  a call:
    void F(a = 0);
    F(.a = 77);

Fundamental matters such as this are best decided early in the design of a language, rather than bolted on afterwards.
>
The funny thing is that my MessageBox example is a C function exported by WinAPI, and I was able to superimpose keyword arguments on top. Since I have to write my own bindings to such functions anyway.
>
The MS docs for WinAPI do tend to show function declarations with fully named parameters, which also seem to be retained in gcc's windows.h (but not in my cut-down one).
 gcc does not have a "windows.h".  You are conflating gcc with some windows packaging of gcc with additional tools, libraries and headers.
Huh? Do you really want to go down that path of analysing exactly what gcc is and isn't? 'gcc' must be the most famous C compiler on the planet!
Yes we all know that 'gcc' /now/ stands for 'gnu compiler collection' or something, and that it is a driver program for a number of utilities. But this is a C group which has informally mentioned 'gcc' for decades across tens of thousands of posts, but you had to bring it up now?
Any viable C compiler that targets Windows, gcc included, needs to provide windows.h.

But it would need defaults added to make it useful:
 Strangely, many people have been able to write code using the MS API without named parameters or defaults.
Yes, and we know what such code looks like, with long chains of mysterious arguments, many of which are zeros or NULLS:
     hwnd = CreateWindowEx(
         0,
         szAppName,
         "Hello, world!",
         WS_OVERLAPPEDWINDOW|WS_VISIBLE,
         300,
         100,
         400,
         400,
         NULL,
         NULL,
         0,
         NULL);
Even without named arguments, just default values, but allowing trailing arguments only to be omitted, those last 4 arguments can be dropped.
(BTW I swapped those first two NULLs around; I guess you didn't notice!)

Let's not pretend that MS's API's are good examples of clear design! (And please don't bother picking other non-MS examples that are the same or worse.)
We all have to use libraries that other people have designed.

Date Sujet#  Auteur
2 Apr 25 * "A diagram of C23 basic types"327Alexis
2 Apr 25 +* Re: "A diagram of C23 basic types"4Lawrence D'Oliveiro
3 Apr 25 i`* Re: "A diagram of C23 basic types"3BGB
3 Apr 25 i `* Re: "A diagram of C23 basic types"2Lawrence D'Oliveiro
4 Apr 25 i  `- Re: "A diagram of C23 basic types"1BGB
2 Apr 25 +* Re: "A diagram of C23 basic types"9Janis Papanagnou
2 Apr 25 i+* Re: "A diagram of C23 basic types"6Kaz Kylheku
3 Apr 25 ii`* Re: "A diagram of C23 basic types"5Janis Papanagnou
3 Apr 25 ii +* Re: "A diagram of C23 basic types"3David Brown
3 Apr 25 ii i`* Re: "A diagram of C23 basic types"2Janis Papanagnou
3 Apr 25 ii i `- Re: "A diagram of C23 basic types"1BGB
3 Apr 25 ii `- Re: "A diagram of C23 basic types"1BGB
2 Apr 25 i+- Re: "A diagram of C23 basic types"1David Brown
3 Apr 25 i`- Re: "A diagram of C23 basic types"1Tim Rentsch
2 Apr 25 +* Re: "A diagram of C23 basic types"307bart
2 Apr 25 i+* Re: "A diagram of C23 basic types"281Muttley
2 Apr 25 ii+* Re: "A diagram of C23 basic types"251David Brown
2 Apr 25 iii+* Re: "A diagram of C23 basic types"247Muttley
2 Apr 25 iiii+* Re: "A diagram of C23 basic types"173Thiago Adams
2 Apr 25 iiiii`* Re: "A diagram of C23 basic types"172Muttley
2 Apr 25 iiiii +* Re: "A diagram of C23 basic types"170bart
2 Apr 25 iiiii i+* Re: "A diagram of C23 basic types"58Muttley
2 Apr 25 iiiii ii+* Re: "A diagram of C23 basic types"5Janis Papanagnou
3 Apr 25 iiiii iii+* Re: "A diagram of C23 basic types"2Janis Papanagnou
3 Apr 25 iiiii iiii`- Re: "A diagram of C23 basic types"1Janis Papanagnou
3 Apr 25 iiiii iii`* Re: "A diagram of C23 basic types"2David Brown
3 Apr 25 iiiii iii `- Re: "A diagram of C23 basic types"1BGB
3 Apr 25 iiiii ii+* Re: "A diagram of C23 basic types"49bart
3 Apr 25 iiiii iii+* Re: "A diagram of C23 basic types"46Kaz Kylheku
3 Apr 25 iiiii iiii+* Re: "A diagram of C23 basic types"43Keith Thompson
3 Apr 25 iiiii iiiii+* Re: "A diagram of C23 basic types"41BGB
3 Apr 25 iiiii iiiiii`* Re: "A diagram of C23 basic types"40Kaz Kylheku
3 Apr 25 iiiii iiiiii +* Re: "A diagram of C23 basic types"36bart
4 Apr 25 iiiii iiiiii i+* Re: "A diagram of C23 basic types"31Lawrence D'Oliveiro
9 Apr 25 iiiii iiiiii ii`* Re: "A diagram of C23 basic types"30Michael S
9 Apr 25 iiiii iiiiii ii +* Re: "A diagram of C23 basic types"21David Brown
9 Apr 25 iiiii iiiiii ii i+* Re: "A diagram of C23 basic types"17BGB
9 Apr 25 iiiii iiiiii ii ii`* Re: "A diagram of C23 basic types"16bart
10 Apr 25 iiiii iiiiii ii ii `* Re: "A diagram of C23 basic types"15David Brown
10 Apr 25 iiiii iiiiii ii ii  +* Re: "A diagram of C23 basic types"5Michael S
12 Apr06:43 iiiii iiiiii ii ii  i`* Re: "A diagram of C23 basic types"4Lawrence D'Oliveiro
12 Apr15:10 iiiii iiiiii ii ii  i +- Re: "A diagram of C23 basic types"1James Kuyper
12 Apr16:21 iiiii iiiiii ii ii  i `* Re: "A diagram of C23 basic types"2David Brown
12 Apr20:27 iiiii iiiiii ii ii  i  `- Re: "A diagram of C23 basic types"1BGB
10 Apr 25 iiiii iiiiii ii ii  +* Re: "A diagram of C23 basic types"4Muttley
10 Apr 25 iiiii iiiiii ii ii  i`* Re: "A diagram of C23 basic types"3bart
10 Apr 25 iiiii iiiiii ii ii  i +- Re: "A diagram of C23 basic types"1Muttley
12 Apr06:44 iiiii iiiiii ii ii  i `- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
10 Apr 25 iiiii iiiiii ii ii  +* Re: "A diagram of C23 basic types"4bart
10 Apr 25 iiiii iiiiii ii ii  i`* Re: "A diagram of C23 basic types"3David Brown
10 Apr 25 iiiii iiiiii ii ii  i `* Re: "A diagram of C23 basic types"2bart
10 Apr 25 iiiii iiiiii ii ii  i  `- Re: "A diagram of C23 basic types"1David Brown
10 Apr 25 iiiii iiiiii ii ii  `- Re: "A diagram of C23 basic types"1BGB
9 Apr 25 iiiii iiiiii ii i`* Re: "A diagram of C23 basic types"3Keith Thompson
12 Apr06:42 iiiii iiiiii ii i `* Re: "A diagram of C23 basic types"2Lawrence D'Oliveiro
12 Apr21:46 iiiii iiiiii ii i  `- Re: "A diagram of C23 basic types"1Keith Thompson
9 Apr 25 iiiii iiiiii ii +* Re: "A diagram of C23 basic types"7Tim Rentsch
9 Apr 25 iiiii iiiiii ii i+* Re: "A diagram of C23 basic types"3Keith Thompson
14 Apr10:10 iiiii iiiiii ii ii`* Re: "A diagram of C23 basic types"2Tim Rentsch
14 Apr12:08 iiiii iiiiii ii ii `- Re: "A diagram of C23 basic types"1Keith Thompson
10 Apr 25 iiiii iiiiii ii i`* Re: "A diagram of C23 basic types"3Michael S
14 Apr09:59 iiiii iiiiii ii i `* Re: "A diagram of C23 basic types"2Tim Rentsch
14 Apr10:44 iiiii iiiiii ii i  `- Re: "A diagram of C23 basic types"1Michael S
9 Apr 25 iiiii iiiiii ii `- Re: "A diagram of C23 basic types"1Kaz Kylheku
4 Apr 25 iiiii iiiiii i`* Re: "A diagram of C23 basic types"4Waldek Hebisch
4 Apr 25 iiiii iiiiii i +* Re: "A diagram of C23 basic types"2Keith Thompson
4 Apr 25 iiiii iiiiii i i`- Re: "A diagram of C23 basic types"1Waldek Hebisch
8 Apr 25 iiiii iiiiii i `- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
3 Apr 25 iiiii iiiiii +* Re: "A diagram of C23 basic types"2Keith Thompson
4 Apr 25 iiiii iiiiii i`- Re: "A diagram of C23 basic types"1Kaz Kylheku
4 Apr 25 iiiii iiiiii `- Re: "A diagram of C23 basic types"1Michael S
4 Apr 25 iiiii iiiii`- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
3 Apr 25 iiiii iiii+- Re: "A diagram of C23 basic types"1Muttley
3 Apr 25 iiiii iiii`- Re: "A diagram of C23 basic types"1Keith Thompson
3 Apr 25 iiiii iii+- Re: "A diagram of C23 basic types"1David Brown
4 Apr 25 iiiii iii`- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
3 Apr 25 iiiii ii`* Re: "A diagram of C23 basic types"3Muttley
4 Apr 25 iiiii ii `* Re: "A diagram of C23 basic types"2Muttley
4 Apr 25 iiiii ii  `- Re: "A diagram of C23 basic types"1Muttley
4 Apr 25 iiiii i`* Re: "A diagram of C23 basic types"111Lawrence D'Oliveiro
4 Apr 25 iiiii i +- Re: "A diagram of C23 basic types"1Keith Thompson
7 Apr 25 iiiii i +* Re: "A diagram of C23 basic types"60candycanearter07
7 Apr 25 iiiii i i+* Re: "A diagram of C23 basic types"58Janis Papanagnou
8 Apr 25 iiiii i ii+- Re: "A diagram of C23 basic types"1candycanearter07
14 Apr05:33 iiiii i ii`* Re: "A diagram of C23 basic types"56Lawrence D'Oliveiro
14 Apr18:40 iiiii i ii `* Re: "A diagram of C23 basic types"55candycanearter07
14 Apr18:46 iiiii i ii  +* Re: "A diagram of C23 basic types"2Kaz Kylheku
15 Apr08:41 iiiii i ii  i`- Re: "A diagram of C23 basic types"1Janis Papanagnou
14 Apr19:36 iiiii i ii  `* Re: "A diagram of C23 basic types"52BGB
14 Apr23:15 iiiii i ii   +- Re: "A diagram of C23 basic types"1Keith Thompson
14 Apr23:33 iiiii i ii   `* Re: "A diagram of C23 basic types"50Lawrence D'Oliveiro
14 Apr23:56 iiiii i ii    +* Re: "A diagram of C23 basic types"30Keith Thompson
15 Apr00:41 iiiii i ii    i+* Re: "A diagram of C23 basic types"19Lawrence D'Oliveiro
15 Apr01:57 iiiii i ii    ii+- Re: "A diagram of C23 basic types"1Keith Thompson
15 Apr04:25 iiiii i ii    ii`* Re: "A diagram of C23 basic types"17James Kuyper
15 Apr05:11 iiiii i ii    ii +* Re: "A diagram of C23 basic types"9Lawrence D'Oliveiro
15 Apr15:06 iiiii i ii    ii i`* Re: "A diagram of C23 basic types"8James Kuyper
15 Apr23:56 iiiii i ii    ii i +* Re: "A diagram of C23 basic types"4Keith Thompson
16 Apr01:04 iiiii i ii    ii i i+- Re: "A diagram of C23 basic types"1Chris M. Thomasson
16 Apr01:53 iiiii i ii    ii i i`* Re: "A diagram of C23 basic types"2James Kuyper
17 Apr16:56 iiiii i ii    ii i i `- Re: "A diagram of C23 basic types"1David Brown
15 Apr23:58 iiiii i ii    ii i `* Re: "A diagram of C23 basic types"3Lawrence D'Oliveiro
15 Apr06:00 iiiii i ii    ii `* Re: "A diagram of C23 basic types"7BGB
15 Apr02:46 iiiii i ii    i`* Re: "A diagram of C23 basic types"10Chris M. Thomasson
15 Apr01:43 iiiii i ii    `* Re: "A diagram of C23 basic types"19BGB
8 Apr 25 iiiii i i`- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
7 Apr 25 iiiii i `* Re: "A diagram of C23 basic types"49bart
4 Apr 25 iiiii `- Re: "A diagram of C23 basic types"1Lawrence D'Oliveiro
2 Apr 25 iiii+* Re: "A diagram of C23 basic types"2Michael S
2 Apr 25 iiii`* Re: "A diagram of C23 basic types"71David Brown
2 Apr 25 iii`* Re: "A diagram of C23 basic types"3Kaz Kylheku
2 Apr 25 ii`* Re: "A diagram of C23 basic types"29Waldek Hebisch
5 Apr 25 i`* Re: "A diagram of C23 basic types"25Philipp Klaus Krause
3 Apr 25 +* Re: "A diagram of C23 basic types"2Michael S
11 Apr 25 +* Re: "A diagram of C23 basic types"2Tim Rentsch
11 Apr 25 `* Re: "A diagram of C23 basic types"2Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal