0
 while(str[i]!='\0')
{
     if(str[i]!=str1[i])
     {
           printf("not equal");
           return 1;
       }
       i++;
}
printf ("equal");
return 0;

What happens here if we use return 1. Will return 1 terminate the if condition or the whole loop?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Please tag a programming language so people can find your question. – John Wu Apr 20 '19 at 05:40
  • 2
    What happens depends on the *scope* where `return` is called. `return` causes control to pickup where it left off immediately before the ***function*** was called. (at the address stored in the frame (or base) pointer pushed onto the stack at the beginning of the function call) If `return` is called in `main`, your program returns control to the shell. If called from a function2 called by another function1, then control returns to funciton1. – David C. Rankin Apr 20 '19 at 05:48
  • So what difference does it make if i use return 0 instead of return 1? – user11386822 Apr 20 '19 at 06:01
  • @user11386822 If you are in the `main` function it makes no real difference (it indicates if you exited the program with an error if it isn't 0). Otherwise it depends on how you use the return value of the function. – ngood97 Apr 20 '19 at 06:04
  • @DavidC.Rankin would you consider converting your comment into an answer? It seems like this is the correct answer and it would be good to recognize it – ngood97 Apr 20 '19 at 06:05
  • Anders already has an answer by example. The crux of the issue is understanding how control is passed to functions which you can get more info on by searching "Assembly Prologue" which will detail what happens from a memory address standpoint. But, Thank You for your vote of confidence `:)` Here is a good link [Function Prologue and Epilogue in C](https://stackoverflow.com/questions/14765406/function-prologue-and-epilogue-in-c) – David C. Rankin Apr 20 '19 at 06:08

1 Answers1

1

it exists the current scope

e.g.

int foo()
{
  return 42;
}


int main()
{
   int n = 0;
   do
   {
     n = foo();
     printf("received %d\n",n); /* will print "received 42" */
   }
   while (n != 42) // will quit since n == 42

   return 0; // returns 0 to the operating system
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • A `return` statement terminates execution of the current **function** and returns control to the function’s caller (C 2018 6.8.6.4 2). It does not merely exit the current **scope**. There may be, and very often are, nested scopes within a function, as blocks of code appear within other blocks of code, most often as parts of `if`, `for`, `while`, and similar statements. A `return` statement does not merely terminate any one of these scopes; it terminates the entire function. – Eric Postpischil Apr 20 '19 at 11:32
  • @EricPostpischil yes I agree, maybe my wording was not exact enough, but it was specific to OPs question "What happens if we use return 1 in if statement that is inside a loop?" and not return statements in general. – AndersK Apr 22 '19 at 15:35