int main( int argc, char ** )
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}
Hab ich gerade in einem alten C-Programm von 1999 gesehen, dass
das jemand gemacht hat. Für manche mag das schlecht wirken weil
es unüblich ist.
int main( int argc, char ** )Det er dårlig fordi det er uvanlig.Det er dårlig fordi det er uvanlig.
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}
Hab ich gerade in einem alten C-Programm von 1999 gesehen, dass
das jemand gemacht hat. Für manche mag das schlecht wirken weil
es unüblich ist.
int main( int argc, char ** )
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}
Am 18.12.2023 um 06:01 schrieb James Kuyper:
Why do you consider it "Unbelievable that that works"? Of course, you
first have to specify what you mean by "works". If argc is zero (which
is permitted), the program does nothing and exits immediately with a
successful exit status. If argc is non-zero, it enters an infinite loop
that does nothing - an implementation is permitted to implement such a
loop as if it did in fact terminate with a successful exit status.
Either way, nothing observable happens other than the exit status, which
means that an implementation is free to implement this as the equivalent
of "int main(void) {}".
I didn't mean that the code does sth. which makes sense but that the
code has a case inside a for inside a switch. Others understood that
better than you.
Am 18.12.2023 um 06:01 schrieb James Kuyper:
Why do you consider it "Unbelievable that that works"? Of course, you
first have to specify what you mean by "works". If argc is zero (which
is permitted), the program does nothing and exits immediately with a
successful exit status. If argc is non-zero, it enters an infinite loop
that does nothing - an implementation is permitted to implement such a
loop as if it did in fact terminate with a successful exit status.
Either way, nothing observable happens other than the exit status, which
means that an implementation is free to implement this as the equivalent
of "int main(void) {}".
I didn't mean that the code does sth. which makes sense but that the
code has a case inside a for inside a switch. Others understood that
better than you.
This has its uses, such as:
switch (state) {
case FOO:
// FOO-only prologue code here, skipped by BAR case.
if (false) {
case BAR:
// BAR-specific prologue code, skipped by FOO case.
}
// fallthrough
case XYZZY:
// code common to FOO, BAR, XYZZY
...
}
Am 19.12.2023 um 08:04 schrieb James Kuyper:
No, I understood that. I was merely pointing out your poor choice of
words. "funktionert" is the past participle of "funktionen", for which
the German language version of Wiktionary provides the definition
"vorschriftsmäßig arbeiten", ...
Idiot.
The wording was correct in German and you're not German.
Am 19.12.2023 um 03:37 schrieb Kaz Kylheku:...
A case/default statement belongs to the closest enclosing switch, no
matter in how many other statements it is enclosed.
...
Do you think it occurred to me with the code I saw that it was also
legal code?
Am 19.12.2023 um 03:37 schrieb Kaz Kylheku:
On 2023-12-18, Bonita Montero <Bonita.Montero@gmail.com> wrote:
Am 18.12.2023 um 06:01 schrieb James Kuyper:
Why do you consider it "Unbelievable that that works"? Of course, you
first have to specify what you mean by "works". If argc is zero (which >>>> is permitted), the program does nothing and exits immediately with a
successful exit status. If argc is non-zero, it enters an infinite loop >>>> that does nothing - an implementation is permitted to implement such a >>>> loop as if it did in fact terminate with a successful exit status.
Either way, nothing observable happens other than the exit status, which >>>> means that an implementation is free to implement this as the equivalent >>>> of "int main(void) {}".
I didn't mean that the code does sth. which makes sense but that the
code has a case inside a for inside a switch. Others understood that
better than you.
A case/default statement belongs to the closest enclosing switch, no
matter in how many other statements it is enclosed.
...
Do you think it occurred to me with the code I saw that it was also
legal code?
Am 19.12.2023 um 08:04 schrieb James Kuyper:
No, I understood that. I was merely pointing out your poor choice of
words. "funktionert" is the past participle of "funktionen", for which
the German language version of Wiktionary provides the definition
"vorschriftsmäßig arbeiten", ...
Idiot.
The wording was correct in German and you're not German.
Am 19.12.2023 um 17:55 schrieb James Kuyper:
Actually, I am 1/4 German. More relevantly, I studied German in school
for a couple of years a few decades ago. I claim no great amount of
fluency - I rely on translate.google.com, though I feel competent to
review it's translations. I would ordinarily very happily defer to the
expertise of a native speaker of German, or even a non-native speaker
who's had more years of study than I have, or more recently. However,
you are so routinely wrong about so many things that I would not trust
you to use German correctly even if you are a native speaker.
However, if you can cite a respectable online dictionary's definition
that supports your use of "functionert" to mean something like "can be
compiled" or "is syntactically correct", please do so.
When you say that sth. "funktioniert" it just means that it works.
It's unbelievable that you deny that compared to a native speaker.
Am 19.12.2023 um 19:34 schrieb David Brown:
'
Do you think only Germans can understand or write correctly in German?
I can, you can't.
On 19/12/2023 18:04, Bonita Montero wrote:
Am 19.12.2023 um 17:55 schrieb James Kuyper:
Actually, I am 1/4 German. More relevantly, I studied German in school
for a couple of years a few decades ago. I claim no great amount of
fluency - I rely on translate.google.com, though I feel competent to
review it's translations. I would ordinarily very happily defer to the
expertise of a native speaker of German, or even a non-native speaker
who's had more years of study than I have, or more recently. However,
you are so routinely wrong about so many things that I would not trust
you to use German correctly even if you are a native speaker.
However, if you can cite a respectable online dictionary's definition
that supports your use of "functionert" to mean something like "can be
compiled" or "is syntactically correct", please do so.
When you say that sth. "funktioniert" it just means that it works.
It's unbelievable that you deny that compared to a native speaker.
It is not your use of language that is at issue - it is your
understanding of the concept "work". You would have provoked the same reaction if you had written "It is unbelievable that this worked" in
English.
Am 19.12.2023 um 17:55 schrieb James Kuyper:,,,
However, if you can cite a respectable online dictionary's definition
that supports your use of "functionert" to mean something like "can be
compiled" or "is syntactically correct", please do so.
When you say that sth. "funktioniert" it just means that it works.
It's unbelievable that you deny that compared to a native speaker.
int main( int argc, char ** )
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed into
`{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed into
`{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is called without arguments.
On 12/20/23 11:34 PM, Ralf Goertz wrote:
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is
called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
The `if (0)` part precedes the first `case` label and for that reason
it is indeed unreachable.
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
Hm, I obviously get that the `if (0)` condition can't ever be fulfilled,
but according to <https://en.cppreference.com/w/cpp/language/statements> selection statements (1,2) the statement after the condition statement
is part of the if statement. So part of the if statement may still be executed. Furthermore, it says -Wswitch-unreachable. That is misleading
imho since it is exactly the switch case label that is reachable.
The `if (0)` part precedes the first `case` label and for that reason
it is indeed unreachable.
I understand that but gcc knows that labeled part might be executed
despite the unfulfilled condition. So it should refrain from warning.
And why are the other `if (0) case x:` statements not complained about?
They live in an else branch of a guaranteed false condition, so they are
not unreachable and that branch will be executed. So gcc should
warn about them, too. No?
Den 2023-12-22 skrev Ralf Goertz <me@myprovider.invalid>:
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
Hm, I obviously get that the `if (0)` condition can't ever be fulfilled,
but according to <https://en.cppreference.com/w/cpp/language/statements>
selection statements (1,2) the statement after the condition statement
is part of the if statement. So part of the if statement may still be
executed. Furthermore, it says -Wswitch-unreachable. That is misleading
imho since it is exactly the switch case label that is reachable.
The `if (0)` part precedes the first `case` label and for that reason
it is indeed unreachable.
I understand that but gcc knows that labeled part might be executed
despite the unfulfilled condition. So it should refrain from warning.
And why are the other `if (0) case x:` statements not complained about?
They live in an else branch of a guaranteed false condition, so they are
not unreachable and that branch will be executed. So gcc should
warn about them, too. No?
I don't know if we have any specific language for having the compiler
warn about the test in the if-statement being unreachable while the
true branch is. Considering this is such a weird edge case, I don't
know if we need it.
As for the statements not warned about, they aren't warned about
because the if (0) tests are reachable via fallthrough from the
previous case labels. The test is reachable via fallthrough while
the branch is reachable via switching to the case labels so all that
code is reachable.
After thinking some more, I relise I don't know if my fallthrough idea
holds water considering the else if construct. The first statement is
false so the second statement should be tested, but we're jumping into
the true branch, which if taken should prevent the other test from
happening. Does anyone have pointers to relevant parts of the standard
that handle this weirdness?
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
Hm, I obviously get that the `if (0)` condition can't ever be fulfilled,
but according to <https://en.cppreference.com/w/cpp/language/statements> selection statements (1,2) the statement after the condition statement
is part of the if statement. So part of the if statement may still be executed.
On 12/17/23 5:27 AM, Bonita Montero wrote:
int main( int argc, char ** )
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed into `{
... }`, one can still stuff a lot into a single protracted statement, including multiple `case` labels.
On 12/22/23 4:24 AM, Andreas Kempe wrote:
After thinking some more, I relise I don't know if my fallthrough idea
holds water considering the else if construct. The first statement is
false so the second statement should be tested, but we're jumping into
the true branch, which if taken should prevent the other test from
happening. Does anyone have pointers to relevant parts of the standard
that handle this weirdness?
Yes, the standard states it explicitly right at the beginning of "8.5.2
The if statement [stmt.if]" (https://timsong-cpp.github.io/cppwp/stmt.if#1)
1 [...] If the first substatement is reached via a label, the condition
is not evaluated and the second substatement is not executed. [...]
On 12/22/23 2:12 AM, Ralf Goertz wrote:
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:Hm, I obviously get that the `if (0)` condition can't ever be fulfilled,
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl;
}
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted
statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
but according to <https://en.cppreference.com/w/cpp/language/statements>
selection statements (1,2) the statement after the condition statement
is part of the if statement. So part of the if statement may still be
executed.
This has absolutely nothing to do with the fulfillability of if's
condition. You can replace `if (0)` with `if (1)` and you will still get
the same warning. The properties of that `if` are completely beside the
point here.
What matters is that the compiler sees some tangible code (i.e. not a no-op declaration, but something that can potentially generate executable instructions), which precedes the very first `case` label. That code is unreachable for obvious reasons, since `switch` will always jump over
it. Hence the warning.
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:I think "if (1)" *is* equivalent to no-op here or, in AT's terms, cannot "potentially generate executable instructions". I would say "there is no
On 12/22/23 2:12 AM, Ralf Goertz wrote:
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:Hm, I obviously get that the `if (0)` condition can't ever be fulfilled, >>> but according to <https://en.cppreference.com/w/cpp/language/statements> >>> selection statements (1,2) the statement after the condition statement
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl; >>>>>> }
As illustrated above, even if the body of switch is not enclosed
into `{ ... }`, one can still stuff a lot into a single protracted >>>>>> statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>>>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
is part of the if statement. So part of the if statement may still be
executed.
This has absolutely nothing to do with the fulfillability of if's
condition. You can replace `if (0)` with `if (1)` and you will still get
the same warning. The properties of that `if` are completely beside the
point here.
I don't find that to be the case. gcc complains about this if (0) being unreachable:
switch (argc)
if (0) case 1: std::cout << 1 << "\n";
but not this one:
switch (argc)
if (1) case 1: std::cout << 1 << "\n";
What matters is that the compiler sees some tangible code (i.e. not a no-op >> declaration, but something that can potentially generate executable
instructions), which precedes the very first `case` label. That code is
unreachable for obvious reasons, since `switch` will always jump over
it. Hence the warning.
I suspect something more subtle is going on.
Ben Bacarisse wrote:
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:I think "if (1)" *is* equivalent to no-op here or, in AT's terms, cannot "potentially generate executable instructions". I would say "there is no
On 12/22/23 2:12 AM, Ralf Goertz wrote:I don't find that to be the case. gcc complains about this if (0) being
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:Hm, I obviously get that the `if (0)` condition can't ever be fulfilled, >>>> but according to <https://en.cppreference.com/w/cpp/language/statements> >>>> selection statements (1,2) the statement after the condition statement >>>> is part of the if statement. So part of the if statement may still be
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Meh... An old example of break-less switch-case branching
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl; >>>>>>> }
As illustrated above, even if the body of switch is not enclosed >>>>>>> into `{ ... }`, one can still stuff a lot into a single protracted >>>>>>> statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of “1” when the program is >>>>>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
executed.
This has absolutely nothing to do with the fulfillability of if's
condition. You can replace `if (0)` with `if (1)` and you will still get >>> the same warning. The properties of that `if` are completely beside the
point here.
unreachable:
switch (argc)
if (0) case 1: std::cout << 1 << "\n";
but not this one:
switch (argc)
if (1) case 1: std::cout << 1 << "\n";
code to reach hence there is no switch-unreachable warning".
Ben Bacarisse wrote:
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
On 12/22/23 2:12 AM, Ralf Goertz wrote:
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
On 12/20/23 11:34 PM, Ralf Goertz wrote:
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
>>>>>> Meh... An old example of break-less switch-case branching >>>>>>
#include <iostream>
int main(int argc, char **)
{
switch (argc)
if (0) case 1: std::cout << 1 << std::endl;
else if (0) case 2: std::cout << 2 << std::endl;
else if (0) case 3: std::cout << 3 << std::endl;
else if (0) default: std::cout << "whatever" << std::endl; >>>>>>> }
As illustrated above, even if the body of switch is not enclosed >>>>>>> into `{ ... }`, one can still stuff a lot into a single protracted >>>>>>> statement, including multiple `case` labels.
Interestingly enough, I get a warning with gcc:
warning: statement will never be executed [-Wswitch-unreachable]
if (0) case 1: std::cout << 1 << std::endl;
although of course I still see the output of ?1? when the program is >>>>>> called without arguments.
The compiler complains specifically about the `if (0)` part, not
about the part that outputs 1.
Hm, I obviously get that the `if (0)` condition can't ever be fulfilled, >>>> but according to <https://en.cppreference.com/w/cpp/language/statements> >>>> selection statements (1,2) the statement after the condition statement >>>> is part of the if statement. So part of the if statement may still be >>>> executed.
This has absolutely nothing to do with the fulfillability of if's
condition. You can replace `if (0)` with `if (1)` and you will still get >>> the same warning. The properties of that `if` are completely beside the >>> point here.
I don't find that to be the case. gcc complains about this if (0) being
unreachable:
switch (argc)
if (0) case 1: std::cout << 1 << "\n";
but not this one:
switch (argc)
if (1) case 1: std::cout << 1 << "\n";
I think "if (1)" *is* equivalent to no-op here or, in AT's terms,
cannot "potentially generate executable instructions". I would say
"there is no code to reach hence there is no switch-unreachable
warning".
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (2 / 14) |
Uptime: | 09:07:00 |
Calls: | 9,822 |
Calls today: | 1 |
Files: | 13,757 |
Messages: | 6,190,746 |