+ 5
Why this code compile?)) (goto)
#include<iostream> using namespace std; int main(){ int a=0; switch(a){ case 0: cout<<"zero"; break; case 1: cout<<"one"; break; case 2: cout<<"two"; break; case 3: cout<<"three"; break; case 4: cout<<"four"; break; case 5: cout<<"five"; break; case 6: cout<<"six"; break; case 7: cout<<"seven"; break; case 8: cout<<"eight"; break; case 9: cout<<"nine"; break; defa7lt: cout<<"it isnt digit"; } } Why this code compile?)
16 Respuestas
+ 4
Lol, It is not compiler error. it's a small feature, which for some reason not explained on most programming courses))
+ 4
OK. I use AIDE 3.2.161216 with C/C++ plugin and DevC++
+ 4
No, compiler no recognize "defa7lt" as "default" and program can't execute
"it isnt digit"
+ 4
No one compiler will not consider it a mistake, because no one compiler will not be against the C++ standard
+ 4
Oh, lol. It isnt a bug, this is clearly described by the C++ standard and name of this is GOTO.
for example:
label1: //it is goto label
switch(0)
{
case 0: //it is switch label becourse 'case' is reserved
cout<<0;
break;
/*...*/
default: //it is switch label too
cout<<1;
break;
defa7lt: //it is goto label becourse this name is not reserved
cout<<3;
}
goto label1; //it is goto
goto defa7lt; //it is goto too
goto default; //it is error becourse goto cant call reserved names
Clear?
+ 4
Of course, defa7lt cant be used via switch. Switch can use only switch-labels (case, default). And if I post goto below the case 1, It will send me to the last phrase constantly (even break not needed)
enjoy:
cout<<"enjoy";
goto enjoy;
=)
+ 2
Good question.
People might say there are two errors -
1. misspelling of default
2. no return although function is int.
1. The compiler sees the defa7lt as label. later you can use this with goto (don't worry you can jump between a switch)
2. the main function is handled differently..
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
explained here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2960.pdf
+ 1
different compilers act differently from others and will react differently. maybe in another compiler or different version, it will react differently
+ 1
the will be bugs and room for improvement. what you can do is email the creator of the compiler and report a bug
+ 1
yes at @fluffy but it did compile the way it was
0
I think it was up to your interpreter. this seems like a compile error, however it will still run but give a warning.
which compiler are you using?
0
I find it strange, but can you please tell me what are you using and the version?
0
When you ask "why this code compile", do you mean why it 'does not' compile?
I can see 2 errors, so I'll be surprised if it does compile:
1. You mispelled 'default'
2. Your main function does not have a return statement.
0
I agree, but if you select the default, did it give you "it isn't digit" ?
0
#include<iostream>
using namespace std;
int main(){
int a=0;
switch(a){
case 0:
cout<<"zero";
break;
case 1:
cout<<"one";
break;
case 2:
cout<<"two";
break;
case 3:
cout<<"three";
break;
case 4:
cout<<"four";
break;
case 5:
cout<<"five";
break;
case 6:
cout<<"six";
break;
case 7:
cout<<"seven";
break;
case 8:
cout<<"eight";
break;
case 9:
cout<<"nine";
break;
default:
cout<<"it isnt digit";
}
return 0;
}
fixed i guess..change the variable for different output.. (Tested)
0
Ok, I now see the issue. You want to know why this code actually compiles and runs, despite the typo in "defa7lt".
The reason is because it does not interpret it as the default case in a switch statement, but as a label (which is never used).
You can easily verify this by changing a to:
int a = 20;
You would now expect the default clause "not a digit" to be outputted to cout, but it isn't, which shows the compiler did not interpret it as the default clause.
Then, to further prove it is interpreted as a label change a to:
int a=1;
Now edit the case 1 as follows :
case 1:
cout << "one" ;
goto defa7lt;
break;
Run and see what happens.