Labels have function scope. You can jump to a label (using the goto statement) from anywhere providing it's defined within the same function as the goto statement.
> using "goto" statement we generally get out of a nested loop structure
If this happens, you should consider breaking your program up into functions. I try to avoid having more than three indentation levels in all my programs.
edit:
@Cronin, Silent: Would you say goto is unnecessary in the case shown below?
char *f(FILE *stream, size_t size)
{
char *s;
if (!(s = malloc(size))) {
errflag = 0;
return 0;
}
if (fread(s, size, 1, stream) != 1) {
errflag = 1;
goto error;
}
if (s[0] != 0x01) {
errflag = 2;
goto error;
}
return s;
error:
free(s);
return 0;
}
edit:
@Silent:
There are situations where it's necessary to use goto, and I'd say that this is certainly one of them. Suppose you increase the number of tests within the function and suppose that the common task performed at exit grows, would it really be beneficial to rewrite the common task for every single test in order to avoid an unconditional jump? An optimising compiler will remove the individual exit points anyway and you'll end up with code that's more or less like what I've written.
The benefits of using goto in this scenario are clear: It prevents you from making errors when you rewrite the common task over and over again, it's easier to modify the common task (since you only have to do it once), and your code will span less lines. I do not see why you would opt for an inferior option in this situation when an unconditional jump is clearly beneficial.