0

I'm a beginner at c. I learnt that goto statement can be used to get out of all nested loops at a time. I also learnt that it is not so preferred in C. However, I use it frequently because I think it helps a lot, and sometimes, it's much easier than the common alternatives. Here is a little program in which I used goto statement to correct the user's mistake instead of using a loop.
So, my question is : Should I really stop using it?

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

int main()
{
    /*A program to store a number in 4 bits only !*/
    printf("Enter x then z :\n");
    int x, y;
    Start:
    scanf("%d %d", &x, &y);
    if((x > 15) || (y > 15) || (x < 0) || (y < 0))
    {
        printf("Wrong numbers! : 0<= x,y <=15\n");
        printf("Enter the numbers again : \n");
        goto Start;
    }
    char z;
    x<<= 4;
    z = x + y;
    printf("z = %d", z);
    return 0;
}
  • If you're writing code for yourself, do whatever you want. If you expect to earn a living writing code, you need to stop using `goto` to create loops. (And you should always check the return value from `scanf`.) – user3386109 Oct 24 '19 at 20:37
  • 1
    **Every non-trivial program contains at least one goto** – wildplasser Oct 24 '19 at 20:38
  • 1
    `goto` is very rarely used by experienced programmers. They would write this using a `while` loop. – Barmar Oct 24 '19 at 20:38
  • @wildplasser The question is about explicit `goto`, not the ones that are implicit in higher-level control structures like `if`, `while`, etc. – Barmar Oct 24 '19 at 20:38
  • 3
    This question touches upon one of the longest-running debates in programming. My opinion is that you (indeed anyone asking the question) should avoid using `goto` until you understand both sides of the debate and can make a well-informed decision of your own. At that point you won't need to ask the question. But until then, go with the herd and avoid it. – High Performance Mark Oct 24 '19 at 20:39
  • There are no absolutes. That said, if you're beginner in a field, and the people with a lot more experience than you give you some advice, its a bit presumptuous for you to think that you know better than them. – dan.m was user2321368 Oct 24 '19 at 20:39
  • I often use gotos in parsers/state machines, adding extra labels to a `for(){switch(){}}` construct, or just after the switch, as an *extra* continue. It can sometimes reduce complexity. – wildplasser Oct 24 '19 at 20:42
  • For all of the absolutists - Linux Kernel style guide has a recommendation on when and how to use it in section 7: https://www.kernel.org/doc/html/v4.10/process/coding-style.html – Eugene Sh. Oct 24 '19 at 20:42
  • 2
    Yes, jumping out of nested loops, or error handling "fast lane", or nested for{switch{}} is often a good use for a goto. – wildplasser Oct 24 '19 at 20:44
  • @Barmar: I said: *non-trivial` ;-) – wildplasser Oct 24 '19 at 20:47
  • I almost never use `goto` -- not because I'm scared of it, not because there's a rule against it, not because I'm afraid people will tease me for using it -- the reason I almost never use it is that I almost never need it. – Steve Summit Oct 24 '19 at 20:48
  • Without `goto`, there would be no glibc... As @wildplasser notes, there a number of instances where the lowly `goto` provides the optimal solution. Knowing when to use it is key. There is nothing wrong with the `goto` expression (like any other expression in C), but it can be wrongly used. – David C. Rankin Oct 24 '19 at 20:49
  • @SteveSummit One can write a program of any complexity without using `goto`. It's just sometimes it helps to clean up the code of unnecessary mess. – Eugene Sh. Oct 24 '19 at 20:50
  • 2
    @wildplasser I've been programming for 40 years, and written many non-trivial programs. I haven't used `goto` in years. – Barmar Oct 24 '19 at 20:51
  • 1
    @EugeneSh. Who are the "absolutists" you're responding to? All the comments have been very qualified. – Barmar Oct 24 '19 at 20:53
  • 1
    As a wise professor once said (paraphrased): "I have written programs using `goto`. I have also used guns. I don't let my children use either." – Steve Summit Oct 24 '19 at 20:54
  • @Barmar The comment above this one? :) Anyway, I meant the future comments as well – Eugene Sh. Oct 24 '19 at 20:55
  • @barmar: me too. And I am using it with satanic pleasure. I **know** that functions will be inlined, but passing two or three state variables (by pointer) to an inlined function (or even worse: a macro) just makes things more brittle. Especially for parsers/state machines. – wildplasser Oct 24 '19 at 20:55
  • It occurs to me that one of the reasons we close questions is to head off unproductive repetetitive debate, so I'm going to (try to) stop commenting here now. – Steve Summit Oct 24 '19 at 20:57
  • @SteveSummit Yes, but we all miss usenet, don't we? – wildplasser Oct 24 '19 at 20:58
  • You should use it when you feel it is right and appropriate to use it. If you don't have that feeling, don't use it. – machine_1 Oct 24 '19 at 21:08
  • A rule of thumb for goto is to never jump *back* in the code. Only jump forward. Your code breaks that rule. But then on the other hand there is a rule of thumb that says you should not use goto at all. Another rule of thumb is that you should not code user interfaces in C. But as with all rules of thumb, you can break them if you have a good enough reason. – klutt Oct 24 '19 at 21:09
  • Example of the jump back is the (old,example) BSD network code: `if errno == EAGAIN) goto again;` [in that case a `continue` could have done the same] – wildplasser Oct 24 '19 at 21:15

1 Answers1

2

You should use goto only when using the alternatives would make the code uglier - or in some extreme cases, performing worse

In your case your code could be written as

for (;;) {
    scanf("%d %d", &x, &y);
    if (x >= 0 && x <= 15 && y >= 0 && y <= 15)
        break;
    printf("Wrong numbers! : 0<= x,y <=15\n");
    printf("Enter the numbers again : \n");
}

without goto, and it would be clearer too because the expression now defines the acceptable values instead of unacceptable.