Thread View: comp.lang.c
412 messages
412 total messages
Page 9 of 9
Started by Lawrence D’Oliveiro
Sat, 10 Oct 2026 00:25
Page 9 of 9 • 412 total messages
Re: Taming The Switch-Statement
Author: Tim Rentsch
Date: Fri, 30 Oct 2026 05:11
Date: Fri, 30 Oct 2026 05:11
28 lines
2989 bytes
2989 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > [ with big snips of lots of irrelevant stuff ] > > On 2026-10-19 17:47, Tim Rentsch wrote: > >>> On 2026-10-18 12:48, David Brown wrote: >>> >>>> The C standards committee collectively reached an agreement counter >>>> to Tim's thoughts, so clearly his preferred variant is /not/ >>>> unquestionably better. >> >> My confidence in the collective wisdom of the ISO C committee has >> declined in recent years, and sadly for more than just the last >> few years. So learning that they have settled (even if that might >> change in the future) on the "label: for(...)" approach is >> disappointing, but not surprising. > > I understand the disappointment when something evolves differently, > where own opinions or own "educated or acquired wisdom" diverges. > (Such expressed disappointment is observable with some other poster > here as well.) > > Opinions are fine, arguments are better. That depends on what might be called an argument. Most of the "arguments" in comp.lang.c are just a long series of statements of opinion.
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 09:13
Date: Sun, 01 Nov 2026 09:13
89 lines
5088 bytes
5088 bytes
On Sun, 18 Oct 2026 23:51:25 -0000 (UTC), (Waldek Hebisch) wrote:
>Dan Cross <> wrote:
>> In article <>,
>> Janis Papanagnou <> wrote:
>>>On 2026-10-18 16:13, Michael S wrote:
>>>> On Sun, 18 Oct 2026 14:34:28 +0200
>>>> Janis Papanagnou <> wrote:
>>>>> On 2026-10-18 14:09, David Brown wrote:
>>>>>>
>>>>>> [...] (I don't know why N3377 proposed "do { } while NAME ( )"
>>>>>> rather than "do NAME { } while ( )". )
>>>>>
>>>>> Oh! - I can only think that it would then be (sort of) "consistent"
>>>>> with the place of the label attacked in a 'while' loop; i.e. behind
>>>>> the 'while' keyword. - It's inconsistent in another interpretation,
>>>>> though; and I'd certainly have favored the latter form, to have the
>>>>> label consistently attached at the loop-entry keyword.
>>>>
>>>> I'd prefer label consistently attached to the loop-exit keyword.
>>>
>>>I can see that this might look appealing in case of 'break'. In
>>>case of the same mechanism working also for 'continue' I'd then
>>>think that it should generally better be an "attribute" of the
>>>loop entry keyword, i.e. placed where loops start. (But that's
>>>just my opinion.) Having it at a different place with a do-loop
>>>I consider a really bad decision. It's advantageous to know the
>>>direction in which to seek for the label if you inspect code. -
>>>But okay, it's "C"; I won't heat up about some future C-release
>>>that I'll never use (especially given that I'm not even be using
>>>all of those more recent additions that are already available).
>>
>> I think colocating it with `do` might create a syntactic
>> ambiguity. In the case of `for`, `while`, and `select`, there
>> is a required token following the keyword (the `(`). However,
>> for a `do`/`while` loop, `do` is followed immediately by a,
>> `secondary-block` production. In particular, you do not _need_
>> braces to denote a compound statement after `do`; something like
>> `do i++; while (i < j);` is perfectly legal in C.
>>
>> In the N3377 proposal, the new `block-name` is just an
>> identifier. If it were allowed immediately after the `do`, then
>> given, `do NAME statement; while(condition);` you can't tell
>> `NAME` from the name of the loop and a random identifier before
>> a statement; something like `do LOOP i++; while (i < j);`
>> appears as though it _would_ be be syntactically valid by N3377
>> as presently worked, but is the programmer's intent to create a
>> loop name, or should this just be a syntax error?
>>
>> By requiring the block-name between a keyword and a
>> syntactically required `(`, you remove that ambiguity: if there
>> is an identifier there, then it is a `block-name`; if not, then
>> the block has no name.
>
>AFAICS this is worse, both:
>
>do (i++); while (i < j);
>
>and
>
>do LOOP(i++); while (i < j);
>
>are currently legal (assuming that 'LOOP' is a function).
>
>Writing statement with parentheses may look silly, but such code
>may be easily produced due to use of macros.
I think for(;;) loop is good because one clear see where are the
initialization part of variable of the loop, the instruction that
break the loop, the change ciclical of variables of the loop
If not so, it is easy emulate the loop in this way
Inizializations
Label: Increment or change varaibles of the loop
Instruction for exit the loop
Instructions
goto label;
It due my ignorance of C, I not see all these problem on all this
LoopName for me is easy that is a label
the position of label is inside the {}
as
for(Ini();f();Inc())
{LoopName:
...
if(cond) break LoopName; // would mean just if(cond) goto LoopName;
}
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 09:17
Date: Sun, 01 Nov 2026 09:17
17 lines
1795 bytes
1795 bytes
On Sun, 01 Nov 2026 10:13:44 +0100, Rosario19 wrote:
>It due my ignorance of C, I not see all these problem on all this
>LoopName for me is easy that is a label
>the position of label is inside the {}
before each loop internal {} instruction
>as
>
>for(Ini();f();Inc())
>{LoopName:
> ...
> if(cond) break LoopName; // would mean just if(cond) goto LoopName;
>}
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 09:27
Date: Sun, 01 Nov 2026 09:27
67 lines
4409 bytes
4409 bytes
On Tue, 13 Oct 2026 12:10:52 +0100, Richard Harnden wrote: >On 13/10/2026 11:26, bart wrote: >> On 13/10/2026 11:12, Janis Papanagnou wrote: >>> On 2026-10-13 09:23, Waldek Hebisch wrote: >>>> [...] >>>> So what those counts indicate: majority of loops is simple, that >>>> is have no early exit. Vast majority of early exits is done >>>> via 'return'. "Interesting" loop control is relatively rare. >>>> Checking which 'break'-s are within equivalent of 'switch' would >>>> require manual inspection and that would be too much work. But >>>> reasonable guestimate to that there are tens such uses. In >>>> a sense that is common, that is significantly more than one >>>> case. If my guestimate is correct, then this may be nontrivial >>>> fraction of all uses of 'break'. OTOH that is tiny fraction >>>> of all loops. >>> >>>  From my (personal) perspective, I commonly have two types of >>> such specific constructs; a very common one is iterating over >>> options >>> >>>   while ((opt = getopts (...)) != -1) >>>     switch (opt) ... >>> >>> where, of course, and quite naturally, always the whole loop >>> is iterated, with no early leaving. And the second common loop >>> type is those where the switched data comes from the function >>> as well >>> >>>   while ((token = parse (...)) != END_TOKEN) >>>     switch (token) ... >>> >>> I'm aware that one might wants to pass and process two values >>> from a function (as I interpreted one of bart's recent code >>> fragments), but then, and if it would be "impossible" to avoid >>> that pattern, I'd just add a condition >>> >>>   int leaving = 0; >>>   while ((token = parse (&token...)) != ERROR && !leaving) >>>     switch (token) >>>       case END_TOKEN: leaving = 1; ... >>> >>> that's really trivial and straightforward, and it's beyond me >>> why anyone (but bart, of course) would see a problem here. >> Because it's a hack. But if this is acceptable, then why does C even >> have loop-break? Since there is always some workaround (or 'writing >> code' as David Brown puts it.) >> >> What would be the consequences of C not having it: would they be >> positive or negative overall? >> >> Now apply the same analysis to a hypothetical loop-break that works >> inside switch. >> > >You could argue that 'break' is just syntactical sugar for 'goto label'. > >If you want to break out of multiple levels, then just use a goto. > >Some people have a pathological hatred of 'goto', sure. There's alway a >way to avoid goto, if that's what you really want. That the code, and >it's intention, is clear is the important thing. > >I really don't see the issue. It's a style thing at most. > I agree
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 09:34
Date: Sun, 01 Nov 2026 09:34
37 lines
2376 bytes
2376 bytes
On Sun, 01 Nov 2026 10:13:44 +0100, Rosario19 wrote:
>I think for(;;) loop is good because one clear see where are the
>initialization part of variable of the loop, the instruction that
>break the loop, the change ciclical of variables of the loop
>
>If not so, it is easy emulate the loop in this way
>
> Inizializations
>Label: Increment or change varaibles of the loop
> Instruction for exit the loop
> Instructions
> goto label;
>
>It due my ignorance of C, I not see all these problem on all this
>LoopName for me is easy that is a label
>the position of label is inside the {}
>as
>
>for(Ini();f();Inc())
>{LoopName:
> ...
> if(cond) break LoopName; // would mean just if(cond) goto LoopName;
>}
I not reach the meaning of break...
so could be this
>for(Ini();f();Inc())
>{
goto L1;
LoopName: break;
L1:
> ...
> if(cond) break LoopName; // would mean just if(cond) goto LoopName;
>}
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 09:39
Date: Sun, 01 Nov 2026 09:39
20 lines
2457 bytes
2457 bytes
On Tue, 13 Oct 2026 21:54:06 -0000 (UTC), Lawrence DÿOliveiro <> wrote: >On Tue, 13 Oct 2026 07:23:40 -0000 (UTC), Waldek Hebisch wrote: > >> So what those counts indicate: majority of loops is simple, that is >> have no early exit. Vast majority of early exits is done via >> 'return'. > >My experience in my own C code is quite the opposite. Lots of loops >have exits in the middle, leading me to use forms like “for (;;)”. I >also try to avoid putting “return” anywhere other than the end of a >function, to make it easier to see the flow of control. > >*cough* Nassi-Shneiderman diagrams *cough* my little experience is this for simple task loop are simple and this is 95% of times than there algo need goto label for optimal construction in terms of speed or number of execution statements
Re: Taming The Switch-Statement
Author: Rosario19
Date: Sun, 01 Nov 2026 10:02
Date: Sun, 01 Nov 2026 10:02
93 lines
4629 bytes
4629 bytes
On Wed, 14 Oct 2026 00:21:00 +0100, bart wrote:
>On 13/10/2026 01:33, Keith Thompson wrote:
>> bart <bc@freeuk.com> writes:
>> [...]
>>> This is my first example expressed in C (note those first three args
>>> are passed by reference):
>>>
>>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>>> switch (pmtype)
>>> case pm_option: ...; break;
>>> case pm_sourcefile:
>>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>>> break;
>>>
>>> Except I can't use break like that. So I have to find a workaround.
>>
>> The code appears to be valid C that doesn't do what you want. I can't
>> easily tell what you want it to do. I don't know which of the two break
>> statements was intended to break out of the loop. A comment would have
>> solved that.
>>
>> I'm going to guess that the first break is supposed to apply to the
>> switch statement, and the second to the while loop.
>>
>> You want the language to be changed to make this kind of thing easier.
>>
>> That's already happening. If you take a look at the N3886 working
>> draft for C2y, it has named break statements. In C2y (or in gcc
>> if you use an extension), the above could be written as:
>>
>> WHILE:
>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>> SWITCH:
>> switch (pmtype)
>> case pm_option: ...; break SWITCH;
>> case pm_sourcefile:
>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>> break WHILE;
>>
>> assuming it's the second "break' that's supposed to break out of
>> the loop. (I'd probably use more meaninful names.)
>>
>> There is already a solution for the problem you're complaining about.
>> (It also solves the problem of breaking out of nested loops.)
>>
>> If you're unable to use this feature, you can use goto. That's not
>> ideal, but with well chosen label names and no backward branching
>> it's not too bad.
>
>I've got a better idea. I added an extra keyword to my C compiler called
>"breakloop". It's like 'break' but always works with the nearest
>enclosing loop, even from inside switch.
>
>So this example (a starts at 0):
>
> while (1) {
> switch (a++) {
> case 1: puts("one"); break;
> case 2: puts("two"); breakloop;
> case 3: puts("three"); break;
> default: puts("other");
> }
>
> if (a>40) break;
> }
>
>finishes after displaying 'two', instead of after the 37th or so 'other'.
>
>Modding the compiler took 90 seconds, and needed 4 extra lines of code.
>Setting up this test took 2-3 minutes.
>
>Simple, yes? Then you think that this could be done even more easily 50
>years ago.
would be equivalent to
%define breakloop goto
while (1) {
goto L1;
LoopName: break;
L1:
switch (a++) {
case 1: puts("one"); break;
case 2: puts("two"); breakloop LoopName;
case 3: puts("three"); break;
default: puts("other");
}
if (a>40) break;
}
Re: Taming The Switch-Statement
Author: bartc
Date: Sun, 01 Nov 2026 12:09
Date: Sun, 01 Nov 2026 12:09
133 lines
5967 bytes
5967 bytes
On 01/11/2026 10:02, Rosario19 wrote:
> On Wed, 14 Oct 2026 00:21:00 +0100, bart wrote:
>> On 13/10/2026 01:33, Keith Thompson wrote:
>>> bart <bc@freeuk.com> writes:
>>> [...]
>>>> This is my first example expressed in C (note those first three args
>>>> are passed by reference):
>>>>
>>>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>>>> switch (pmtype)
>>>> case pm_option: ...; break;
>>>> case pm_sourcefile:
>>>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>>>> break;
>>>>
>>>> Except I can't use break like that. So I have to find a workaround.
>>>
>>> The code appears to be valid C that doesn't do what you want. I can't
>>> easily tell what you want it to do. I don't know which of the two break
>>> statements was intended to break out of the loop. A comment would have
>>> solved that.
>>>
>>> I'm going to guess that the first break is supposed to apply to the
>>> switch statement, and the second to the while loop.
>>>
>>> You want the language to be changed to make this kind of thing easier.
>>>
>>> That's already happening. If you take a look at the N3886 working
>>> draft for C2y, it has named break statements. In C2y (or in gcc
>>> if you use an extension), the above could be written as:
>>>
>>> WHILE:
>>> while (pmtype = nextcmdparam(¶mno, &name, &value, "m") {
>>> SWITCH:
>>> switch (pmtype)
>>> case pm_option: ...; break SWITCH;
>>> case pm_sourcefile:
>>> if (passlevel == run_pass || passlevel == runpcl_pass) {
>>> break WHILE;
>>>
>>> assuming it's the second "break' that's supposed to break out of
>>> the loop. (I'd probably use more meaninful names.)
>>>
>>> There is already a solution for the problem you're complaining about.
>>> (It also solves the problem of breaking out of nested loops.)
>>>
>>> If you're unable to use this feature, you can use goto. That's not
>>> ideal, but with well chosen label names and no backward branching
>>> it's not too bad.
>>
>> I've got a better idea. I added an extra keyword to my C compiler called
>> "breakloop". It's like 'break' but always works with the nearest
>> enclosing loop, even from inside switch.
>>
>> So this example (a starts at 0):
>>
>> while (1) {
>> switch (a++) {
>> case 1: puts("one"); break;
>> case 2: puts("two"); breakloop;
>> case 3: puts("three"); break;
>> default: puts("other");
>> }
>>
>> if (a>40) break;
>> }
>>
>> finishes after displaying 'two', instead of after the 37th or so 'other'.
>>
>> Modding the compiler took 90 seconds, and needed 4 extra lines of code.
>> Setting up this test took 2-3 minutes.
>>
>> Simple, yes? Then you think that this could be done even more easily 50
>> years ago.
>
>
> would be equivalent to
>
> %define breakloop goto
>
> while (1) {
> goto L1;
> LoopName: break;
> L1:
> switch (a++) {
> case 1: puts("one"); break;
> case 2: puts("two"); breakloop LoopName;
> case 3: puts("three"); break;
> default: puts("other");
> }
> if (a>40) break;
> }
>
That would be too intrusive. Simpler is just:
#define breakloop goto
while (1) {
switch (a++) {
case 1: puts("one"); break;
case 2: puts("two"); breakloop LoopName;
case 3: puts("three"); break;
default: puts("other");
}
if (a>40) break;
}LoopName:
Simplest is 'goto LoopName;', but then you lose the information that
this is breaking out of the enclosing loop; that could be jumping anywhere.
The 'stepping over' part might anyway be better written as:
if (0){LoopName: break;}
or disguised via a macro, for example:
#define LOOP(name) if(0){name: break;}
#define breakloop goto
int a=0;
while (1) {
LOOP(LoopName)
switch (a++) {
case 1: puts("one"); break;
case 2: puts("two"); breakloop LoopName;
case 3: puts("three"); break;
default: puts("other");
}
if (a>40) break;
}
But overall this is a poor way to get around language shortcomings.
Re: Taming The Switch-Statement
Author: Lawrence D’Oliveiro
Date: Mon, 02 Nov 2026 02:49
Date: Mon, 02 Nov 2026 02:49
24 lines
2722 bytes
2722 bytes
On Sun, 01 Nov 2026 10:39:47 +0100, Rosario19 wrote: > On Tue, 13 Oct 2026 21:54:06 -0000 (UTC), Lawrence DÿOliveiro <> > wrote: > >> On Tue, 13 Oct 2026 07:23:40 -0000 (UTC), Waldek Hebisch wrote: >> >>> So what those counts indicate: majority of loops is simple, that >>> is have no early exit. Vast majority of early exits is done via >>> 'return'. >> >> My experience in my own C code is quite the opposite. Lots of loops >> have exits in the middle, leading me to use forms like “for (;;)”. >> I also try to avoid putting “return” anywhere other than the end of >> a function, to make it easier to see the flow of control. >> >> *cough* Nassi-Shneiderman diagrams *cough* > > my little experience is this for simple task loop are simple and > this is 95% of times than there algo need goto label for optimal > construction in terms of speed or number of execution statements If I understand that correctly, you are saying that in 95% of cases where you didn’t need break, you were able to make do without it.
Re: Taming The Switch-Statement
Author: Lawrence D’Oliveiro
Date: Mon, 02 Nov 2026 02:50
Date: Mon, 02 Nov 2026 02:50
5 lines
1583 bytes
1583 bytes
On Sun, 01 Nov 2026 11:02:07 +0100, Rosario19 wrote: > %define breakloop goto What exactly is that supposed to do?
API/ABI (Was: Taming The Switch-Statement)
Author: bartc
Date: Fri, 27 Nov 2026 16:29
Date: Fri, 27 Nov 2026 16:29
58 lines
3499 bytes
3499 bytes
On 21/10/2026 23:55, Dan Cross wrote: > In article <11bbd8k$bhfq$1@dont-email.me>, bart <bc@freeuk.com> wrote: >> On 21/10/2026 21:36, Dan Cross wrote: >>> In article <11bb47i$889c$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>>> On 21/10/2026 19:38, Dan Cross wrote: >>>>> In article <11baur1$63id$1@dont-email.me>, bart <bc@freeuk.com> wrote: >>>> >>>>>> So, now tell me what I've got wrong. >>>>> >>>>> Wow. You really, truly, have no idea how any of this works. I >>>>> don't even know where to begin. You just throw around words but >>>>> have no idea what they mean in context. >>>>> >>>>> It's breathtaking, really. >>>> >>>> And Wow, your reply is exactly the kind I expected! >>>> >>>> Of course, you're not going to tell me what API means versus ABI, and >>>> you're not going to answer my question about what I've got wrong. >>> >>> Honestly? No, I'm not. It's clear that if I tried, you would >>> either not understand, or deliberately ignore it so you could >>> blindly continue in your self-imposed ignorance. >>> >>>> Because actually you don't know. >>> >>> Umm..... >>> >>>> This was apparent from things you've said in the past. >>> >>> You know, it's one thing to be ignorant yourself. It's quite >>> another to project that onto others. >> >> I've seen zero evidence that you know anything about the matter. Just >> bluster. > > And sadly, I've seen plenty of evidence confirming that you know > nothing about it at all. > [About APIs vs ABIs] So I've finally got the courage to look at this post. And ... it's nothing. Just more bluster. Disappointing really; I expected it to be more savage. I mean, you /could/ be right: maybe I've somehow done implementations that have strictly conformed to platform ABIs, and have worked with countless APIs by designing them, converting them, generating them, using them, while having no idea which one is which or what either of them mean. And maybe you /could/ be an expert in these matters, but have chosen not to answer my technical questions or point out where I've got things wrong, because I might not understand your big words. Or we could be realistic!
Page 9 of 9 • 412 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads