i have a doubt in my code it goes to infinite loop.

Started by nitinainani, June 19, 2013, 08:58:44 PM

Previous topic - Next topic

nitinainani

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

Quote from: nitinainani on June 19, 2013, 08:58:44 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

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;
}




frankie

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

Quote from: nitinainani on June 20, 2013, 01:26:17 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

Your not working function is equivalent to:

int count(int i)
{
   return i; 
   i = i+1; // too late
}

nitinainani

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.

       }