Faulty default!

A

Anjali M

#include<stdio.h>
int main()
{
int a = 10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}

If you think, this will print NONE, look carefully. The 'default' is
misspelled. I compiled this on gcc-2.95. Why doesnt the compiler
complain that this program is faulty? Why does it accept this program
as a valid one?

Thanks in advance,
Anjali.
 
D

Dominik Zanettin

Why doesnt the compiler
complain that this program is faulty? Why does it accept this program
as a valid one?

because 'defa1ut:' is a valid lable. you could jump to it with goto.

regards
zhan
 
R

Richard Bos

switch(a)
{
defa1ut:
printf("NONE\n");
}
If you think, this will print NONE, look carefully. The 'default' is
misspelled. I compiled this on gcc-2.95. Why doesnt the compiler
complain that this program is faulty? Why does it accept this program
as a valid one?

Because it _is_ a valid program. defalut: is just another label. You
could even goto it, should you feel adventurous. Odd, perhaps, but true.

Richard
 
D

Darrell Grainger

#include<stdio.h>
int main()
{
int a = 10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}

If you think, this will print NONE, look carefully. The 'default' is
misspelled. I compiled this on gcc-2.95.
Why doesnt the compiler complain that this program is faulty?

If you have a valid symbol ending with a colon the compiler will assume
it is a label. I can put as many labels as I want in a program. They just
happen to look a little like a case statement, especially when you stick
it in the middle of a switch/case block. In other words, this program is
not faulty.

If you turned on warnings you might get the compiler warning you that you
have a label that you never used. It might even warn you that there is no
default case in the switch statement. But there is nothing in the C
standard indicating that it must do this.
Why does it accept this program as a valid one?

Because it sees the defalut: as a label to be used by a goto statement. It
is a valid C program. It is just not what you intended.
 
A

Anjali M

If you have a valid symbol ending with a colon the compiler will assume
it is a label. I can put as many labels as I want in a program. They just
happen to look a little like a case statement, especially when you stick
it in the middle of a switch/case block. In other words, this program is
not faulty.

If you turned on warnings you might get the compiler warning you that you
have a label that you never used. It might even warn you that there is no
default case in the switch statement. But there is nothing in the C
standard indicating that it must do this.


Because it sees the defalut: as a label to be used by a goto statement. It
is a valid C program. It is just not what you intended.

Okay, I get it now.
Thanks everybody,
Anjali
 
A

Anjali M

If you have a valid symbol ending with a colon the compiler will assume
it is a label. I can put as many labels as I want in a program. They just
happen to look a little like a case statement, especially when you stick
it in the middle of a switch/case block. In other words, this program is
not faulty.

If you turned on warnings you might get the compiler warning you that you
have a label that you never used. It might even warn you that there is no
default case in the switch statement. But there is nothing in the C
standard indicating that it must do this.


Because it sees the defalut: as a label to be used by a goto statement. It
is a valid C program. It is just not what you intended.

Okay, I get it now.
Thanks everybody,
Anjali
 
A

Anjali M

If you have a valid symbol ending with a colon the compiler will assume
it is a label. I can put as many labels as I want in a program. They just
happen to look a little like a case statement, especially when you stick
it in the middle of a switch/case block. In other words, this program is
not faulty.

If you turned on warnings you might get the compiler warning you that you
have a label that you never used. It might even warn you that there is no
default case in the switch statement. But there is nothing in the C
standard indicating that it must do this.


Because it sees the defalut: as a label to be used by a goto statement. It
is a valid C program. It is just not what you intended.

Okay, I get it now.
Thanks everybody,
Anjali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top