#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 2101
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: 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:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |