NO

Author Topic: i have a doubt in my code it goes to infinite loop.  (Read 3944 times)

nitinainani

  • Guest
i have a doubt in my code it goes to infinite loop.
« on: June 19, 2013, 08:58:44 PM »
Hi,

Following is my code which i wrote and got infinite loop.

#include<stdio.h>
#include<conio.h>



int count(int i)
{
   return i++;   // this is a problem if I change it to i=i+1 it works I want to know why it happens?

}


int main(void)
{
       clrscr();  // this is to clear screen from previously run programs

       for(int i=0 ; i<10 ; i = count(i) )    // I am trying to increment counter by calling function.
       {
      printf(" count is %d",i);

       }
       getch();  // this is to pause screen in turbo c++
       return 0;
}




Thanks.

czerny

  • Guest
Re: i have a doubt in my code it goes to infinite loop.
« Reply #1 on: June 19, 2013, 10:57:21 PM »
return i++;   // this is a problem if I change it to i=i+1 it works I want to know why it happens?
when the function returns and i is evaluated, it is not yet incremented.

try ++i

i is first incremented and afterwards evaluated.

nitinainani

  • Guest
Re: i have a doubt in my code it goes to infinite loop.
« Reply #2 on: June 20, 2013, 01:26:17 AM »
Thanks Czerny!!


I know it works with ++i  but if  I use i++ and call it inside for loop and assign it to variable ,it also works and increments the value properly how?

It works when I assign value returned by count(i)  to variable j  I have shown it as following:


#include<stdio.h>
#include<conio.h>



int count(int i)
{
   return i++;   // this is a problem if I change it to i=i+1 it works I want to know why it happens?

}


int main(void)
{
       clrscr();  // this is to clear screen from previously run programs
       
        int j=0;
       
       for(int i=0 ; i<10 ; i++ )   
       {
               j=count(i);   // here it gives proper value
             
               printf(" count is %d",j);   // how this works if value of i which incremented later .

       }
       getch();  // this is to pause screen in turbo c++
       return 0;
}




Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: i have a doubt in my code it goes to infinite loop.
« Reply #3 on: June 20, 2013, 09:15:10 AM »
The form i++ is called post-increment, meaning that the increment is done 'after' the variable use.
In your case the 'use' of the variable is to return its value, only after that the variable could be incremented. But because is a no sense to change something in an already terminated function the compiler optimizer simply doesn't increment the variable (the optimizer is *not* intended as code optimization level, but the standard code finalizer of the compiler: you could change this behaviour).
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

  • Guest
Re: i have a doubt in my code it goes to infinite loop.
« Reply #4 on: June 20, 2013, 09:41:41 AM »
       for(int i=0 ; i<10 ; i++ )   
       {
               j=count(i);   // here it gives proper value
             
               printf(" count is %d",j);   // how this works if value of i which incremented later .

       }
here i is incremented by the for loop and not your function. What do you mean with 'proper value'? What do you expect?

In the function 'count' i is local. So if the compiler would produce code to increment i that would be useless because the local i gets lost afterwards.
But in reality the function terminates with the return. The increment will not be done.

czerny

  • Guest
Re: i have a doubt in my code it goes to infinite loop.
« Reply #5 on: June 20, 2013, 09:44:38 AM »
Your not working function is equivalent to:
Code: [Select]
int count(int i)
{
   return i; 
   i = i+1; // too late
}

nitinainani

  • Guest
Re: i have a doubt in my code it goes to infinite loop.
« Reply #6 on: June 20, 2013, 04:21:51 PM »
Thanks a   lot!!!  Czerny  and Frankie!


I got  what you two are saying.


 for(int i=0 ; i<10 ; i++ )   
       {
               j=count(i);   // here it gives proper value
             
               printf(" count is %d",j);   //Here j is incremented by local variable i which in turn is passed to count and simply returned
                                                      //back   the post incremented  i++ (whose value is incremented after t is returned )in count function
                                                      //just passes back the value to j.

       }