Veera
Veera

Reputation: 33

If I write a label without goto statement in my program what will happen?

If I write a label without using a goto statement, then the program runs in the program's flow. Why doesn't the compiler throw an error for the label?

#include<stdio.h>

int print(int a);

main()
{
    int a = 7;
hhh:
    print(a);
    if (a == 0)
        return;
    else
        --a;
    return;
}

int print(int a)
{
    printf("%d", a);
}

Upvotes: 0

Views: 1596

Answers (2)

Since the compiler cannot know if there is a logic error in that, it will not raise an error, it might give you a warning tough. But haveing a lavel does not break your code so it should be allowed to compile and run without any issues.

Upvotes: 0

the busybee
the busybee

Reputation: 12600

You can define as many unused labels, variables, functions as you like. Unless you tell your compiler to use the highest possible warning level and to treat all warnings as errors, it will compile just fine.

Syntactically it's not an error to have unused stuff; it's commonly just bad style.

Upvotes: 1

Related Questions