#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. 7 2100
On Mon, 05 Jul 2004 04:30:36 -0700, Anjali M wrote: 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 g_********@rediffmail.com (Anjali M) wrote: 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
On Mon, 5 Jul 2004, Anjali M wrote: #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.
Thanks in advance, Anjali.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Dominik Zanettin wrote: because 'defa1ut:' is a valid lable.
Am I allowed to add this one to my collection of nice funny quotes ?
:-) da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) wrote in message news:<Pi*******************************@drj.pf>... On Mon, 5 Jul 2004, Anjali M wrote:
#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.
Thanks in advance, Anjali.
Okay, I get it now.
Thanks everybody,
Anjali da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) wrote in message news:<Pi*******************************@drj.pf>... On Mon, 5 Jul 2004, Anjali M wrote:
#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.
Thanks in advance, Anjali.
Okay, I get it now.
Thanks everybody,
Anjali da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) wrote in message news:<Pi*******************************@drj.pf>... On Mon, 5 Jul 2004, Anjali M wrote:
#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.
Thanks in advance, Anjali.
Okay, I get it now.
Thanks everybody,
Anjali This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: Siv |
last post by:
Hi,
I just discovered that if in an ADO.NET query I use:
"Select * from Invoices Where InvoiceDate BETWEEN StartDate AND EndDate;"
In this case StartDate would be 1st of month and EndDate...
|
by: Neil Cerutti |
last post by:
How do I cope with faulty encoding settings?
I'm writing an application that needs all internal character data
to be stored in iso-8859-1. It also must allow input and output
using stdin and...
|
by: shana07 |
last post by:
What's that mean by faulty program in java? I am told that it is any wrong in calculation for numerical program. Really appreciate if someone could give examples here to clarify about this topic. ...
|
by: Zytan |
last post by:
I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
by: Nimmi Srivastav |
last post by:
Apologies if my cross posting has offended anyone....
For a pure hobbyist C/C++ programmer, who wants to develop
applications to run on Windows, what would be a better choice to
install: Visual...
|
by: ashitpro |
last post by:
There are 7 machines, each of them produces the coin of weight 2 grams.
Someday somehow one machine gets faulty and started producing coin of weight 1 gram. All coins looks similar in dimensions....
|
by: =?Utf-8?B?ZGlhbmE=?= |
last post by:
I do not seem to be able to highlight any portion of a word doc using the
mouse. As such, I can't do any work on Word.
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |