break;
The break is used to (1)terminate a case in the switch statement. (2)Force an immediate termination of a loop.

sample prog.

void main()
{
    int a;
    printf("1. Happy\n");
    printf("2. Sad\n");
    printf("Enter Your Choice..");
    scanf("%d",a);
    switch(a)
    {
       case 1: {
                 printf("You are happy.");
                 break;
               }
       case 2: {
                 printf("You are sad.");
                 break; 
               }
       default:{
                 printf("Oops you error..");
                 break;
               }
    }
  getch();
}

The program is wrong. Here's why:

First of all, you are not including a header file, therefore, you have no access to the I/O functions (i.e. scanf ).

Next, you define main as void main() , when it should be int main(void) .

Furthermore, you write scanf("%d", a); . This is incorrect, as the value of a must be changed by the function. Therefore, you must feed it a pointer to a: scanf("%d", &a); .

You don't need braces around the statements inside a case statement. So, instead of

case 1: {
printf("You are happy.");
break;
}

we write

case 1:
    printf("You are happy.");
    break;

Last but not least, your function is not returning anything. It should return 0 upon successful execution. So instead of getch(); , use return 0; .

@creeps

A few comments...

>First of all, you are not including a header file, therefore, you have no access to the I/O functions (i.e. scanf ).

This is an interesting observation, considering he probably only copied the main part of the function and not the whole file. Especially considering that his question was so basic, it would be redundant information to add the header files just to ask about the function of "break".

Next, you define main as void main() , when it should be int main(void) .

...your function is not returning anything. It should return 0 upon successful execution

You are absolutely right that all C programs should return an int and not a void, but he shouldn't be returning 0 if the return type is void. I just wanted to clarify that because you made it sound like it was an option to return 0 on a void.

You don't need braces around the statements inside a case statement.

I would just like to add that you DO need braces around the statements inside a case IF you are declaring a variable inside the case block. Your advice would have caused compiler errors had you not mentioned that.

Either way, awesome post!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.