-5

Can I use Goto to jump into other functions? for example

void x(){
 printf("hello");
}

void y(){
 printf("hi");
}

int main(){     /*assume that all var are declared */

 scanf("%d",&input);
  if(input == 1) goto y();
  else(input == 2) goto x();
}
PNC
  • 357
  • 1
  • 5
  • 19
  • 1
    Did you try compiling that? – jwrush Aug 23 '13 at 18:04
  • 2
    Pointer - > `goto` isn't an function its an jumping statement - rather say an `evil` statement – exexzian Aug 23 '13 at 18:12
  • 3
    @sansix: "evil" is pure rhetoric, `goto` has its uses just like assembler has its uses. – Dietrich Epp Aug 23 '13 at 18:14
  • 1
    it's REAL uses are much more limited than what it actually gets used for though. the big problem with using goto in your code (although it may not be as apparent from this example) is it makes your code difficult to maintain. Think for example, if you have a program that's some 30,000 lines of code, and there's gotos all over the place. You will be jumping up and down in the code in what's not necessarily a logical way to figure out what the heck it's actually doing. – user2366842 Aug 23 '13 at 18:24

4 Answers4

3

You cannot use goto to branch into a different function; a goto may only branch within the current function. You would simply call the functions x() and y().

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

In most cases, goto is a bad idea, and can usually be avoided. In this example, it could be done as such:

if (input == 1)
{
 y();
}
else
{
 x();
}
user2366842
  • 1,231
  • 14
  • 23
0

From your question it appears that you are learning C and you are a newbie in programming in general. If this is the case, I strongly suggest you to follow a good book. C is not a good language for a newbie to simply learn by doing. It has tons of pitfalls and it is very easy to add a comma or semicolon in the wrong place and have a perfectly functioning (i.e. compiling and running) program that does nothing of what you expect! C doesn't hold your hand like, say, Java or Python.

As for your question, this reference could be useful.

Another advice: play with goto to learn its syntax and semantics, then try to avoid using it in "real code" until you become an expert C programmer. Almost any program can be created without gotos. When an expert programmer resorts to gotos is probably because either:

  • it's implementing some optimization trick, or
  • the algorithm at hand is so tricky that falls in one of those rare cases where goto makes the code more readable.
  • It's not at all "rare" for a `goto` to make things more readable in C. It is the most readable and least error-prone way to do error handling in C. grep Linux kernel source for "goto" to get an idea. – asveikau Aug 24 '13 at 18:30
  • @asveikau Sorry for the late reply (I missed somehow your comment). You're right, I probably choose the wrong words. I meant to highlight that the uses of goto are somewhat "special" (I took the beginner POV - alas beginners are rarely shown good error handling techniques in textbooks and basic courses), and good error handling *is tricky*. – LorenzoDonati4Ukraine-OnStrike Aug 28 '13 at 19:02
-2

Do NOT use goto, ever. Its ugly, old and obfuscates things unnecessarily.

int main() {     /*assume that all var are declared */

    scanf("%d",&input);
    if(input == 1) {
        y();
    } else if (input == 2) {
        x();
    }
}  
user1231232141214124
  • 1,339
  • 3
  • 16
  • 22
  • 6
    No. `goto` **has** its legitimate uses, e.g. it can be used to break out from nested loops efficiently, i.e. without using extra variables, or to code state machines. Its **abuse** is wrong, i.e. you should avoid it for any task that can be accomplished with normal structured control flow statements. It is like a jackhammer: you *could* tear a concrete wall down without it, but it is the right tool for the job in the hands of the expert who knows what he is doing. – LorenzoDonati4Ukraine-OnStrike Aug 23 '13 at 18:30
  • Operative word being **expert**, which OP is far from. – user1231232141214124 Aug 23 '13 at 18:36
  • 1
    This is not an answer to the question, just a polemic that some people start as soon as they see the word `goto`. – Jens Gustedt Aug 23 '13 at 18:50
  • @JensGustedt there's a pretty good reasoning that answers like this are drawn, especially to those new to programming (appears the OP is in this case). I think the reason goto's get smacked down this quickly to new programmers is because of the serious potential of incorrect use/abuse. – user2366842 Aug 23 '13 at 19:12
  • @user2366842, this is actually a fight that goes on since decades. There is good reasoning on both sides and a lot of ideology, polemic and recurs to "authory", too. Please don't start these wars here, again. SO is not the place for such a thing. – Jens Gustedt Aug 23 '13 at 19:34
  • -1 for a response that sounds like it is just spewing back something the poster has heard before. – Ed S. Aug 23 '13 at 23:03