473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

switch in main?

bob
I have a switch in a main function:

int main() {

switch ( 5 ) {

case 1 :
// Process for test = 1
//...
break;

case 5 :
// Process for test = 5
//...
break;

default :
// Process for all other cases.
//...

}

return 0;
}

But when I compile I get:

error: expected primary-expression before '}' token
error: expected `;' before '}' token

Is it illegal to have a switch in a main function?

Oct 25 '07 #1
1 1391
"bob" <tu*****@gmail.comwrote in message
news:11**********************@o38g2000hse.googlegr oups.com...
>I have a switch in a main function:

int main() {

switch ( 5 ) {

case 1 :
// Process for test = 1
//...
break;

case 5 :
// Process for test = 5
//...
break;

default :
// Process for all other cases.
//...

}

return 0;
}

But when I compile I get:

error: expected primary-expression before '}' token
error: expected `;' before '}' token

Is it illegal to have a switch in a main function?
Yes. Your compiler is acually bitching about:

default: }

You have no statement after the break: statement, and it was expecting one.
Change it to:

default:
// Process for all other cases.
// ...
break;
}

and it should compile.

In face even an empty statment would work.

default:
;
}

Oct 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
13
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
19
by: Bruintje Beer | last post by:
Hello, I have a html page with a main picture but I want the main picture switch every 3 seconds to a new main pictures. How can I do this in java script. John
9
by: PhreakRox | last post by:
The ToClose switch in this program is not working as expected, it allways returns a null value, if anyone knows a way to fix up the code, or can suggest a better method of doing so, your help would...
0
by: Sudz28 | last post by:
Greetings! I am attempting to write a program that will allow a user to manipulate data read from a file, and am obviously in no way near finished. However, one problem I'm having that I don't...
7
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include...
7
by: Chad | last post by:
The program: #include <stdio.h> void hello(void) { printf("hello \n"); } void hi(void) { printf("hi \n");
9
by: Cybex | last post by:
I am trying to get this to work but when ever I enter an proper integer it just hangs. The Switch default seems to catch the improper integers but the right ones are not triggering the way I...
6
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
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...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.