Teja
Teja

Reputation: 13524

Break statement not within loop or switch in C

I am getting this error in my C code. I don't know what I am doing wrong. If I comment this code my program works. This piece of code is inside int main().

 if(argc!=2 && strcmp(argv[0],"selection-sort")==0 && strcmp(argv[1],"input.txt")==0 && strcmp(argv[2],"output.txt")==0)
        {
                printf("The command line arguments are correct.\n");
        }
        else
        {
                printf("The command line arguments are wrong.I am exiting.\n");
                break;
        }

Upvotes: 1

Views: 75124

Answers (6)

Suraj K Thomas
Suraj K Thomas

Reputation: 5883

'break' will only get you out of the innermost loop or switch. You can use 'return' to exit out of a function at any time.

"A break statement may appear only in an iteration statement or a switch statement, and terminates execution of the smallest enclosing such statement".

And it makes sense too - you can "escape" from a method with "return" , and you can skip code in other situations with an if/else. I don't know what a "break" outside a case would be expected to do.

'break' is really only a restricted form of 'goto' anyway. Ideally, you want a single point of exit from any block of code. You should really only use 'break' in a switch statement because that is the only way to make it work. In any other context, there are better ways to accomplish the same thing. The same applies to 'continue'.

Upvotes: 1

fabiorocha
fabiorocha

Reputation: 150

Break is supposed to be used in loops.

Use a return statement, which causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called (return address).

Upvotes: 3

paldepind
paldepind

Reputation: 4870

The other answers are correct, this is just a slight addition.

To return probably in this specific case you should include errno.h like this:

#include <errno.h>

And furthermore return like this:

return EINVAL;

Then you are signaling that the program is terminating due to an error and the return value specifically states that the error is invalid arguments.

Upvotes: 1

Keith Miller
Keith Miller

Reputation: 1347

First of all make sure you are including the needed header files:

#include <stdio.h>
#include <stdlib.h>

The break command is used for exiting loops, you are not in a loop you are just in an else statement, you have nothing to break from. The code flow executes normally after passing that else statement. If you want to exit the program in that else statement you could do something like this:

    else
    {
            printf("The command line arguments are wrong.I am exiting.\n");
            return 1; //exit program with status 1 to indicate a non normal exit
    }

Or if you want to continue the program after displaying that message you could just do this:

else printf("The command line arguments are wrong.I am exiting.\n");
//more code here

You only use break in loops like so:

while(foo)    //while foo is true
{
    break;    //exit the loop
}

Upvotes: 4

cnicutar
cnicutar

Reputation: 182619

The way it looks I think you're not in a loop but just checking args in main. You probably want something like return 1 or exit(1) instead of the break.

Upvotes: 11

Tudor
Tudor

Reputation: 62439

The error message in the title says it all: break can only be used to exit a loop or prevent a case from falling through. MSDN quote:

The break statement terminates the execution of the nearest enclosing do, for, switch, or while statement in which it appears.

To leave a function use return.

Upvotes: 3

Related Questions