My Question is regarding the code written below. If you look at the function called "function"
when 'a' is passed to the function and the function "function" is evaluated. I was wondering what the return value would be for the first pass when (a =2), my questin specifcaly is that do we increment b and c since its post? otherwise if they had been pre increment would the value of the expression 'a1+b+c' be any different ? thanks.

int function( int a1 )
{
      int b = 0;
      int c = 0;
      b++;
      c++;
return ( a1 + b + c );
}

int main() /*main function*/
{
  int a = 2, i;

  for( i=0; i<3; i++ )
    printf( "%d ", function(a) );
  return 0;
}

Recommended Answers

All 3 Replies

>>otherwise if they had been pre increment would the value of the expression 'a1+b+c' be any different
No they are the same. pre and post increment only has any real difference if used in an expression of some sort. For example

int a = 1;

foo(++a);
foo(a++);

In the code above the value of a inside function foo() will be different because in the first instance the value of a is first incrmeneted then that value is passed to foo(). In the second instance the current value of a is passed to foo() then incremented after foo() returns to its caller.

This goes the same for a "For Loop"??, the increment expression, if its pre/post wouldnt it be the same since its being evaluated after the statements of the loop have been executed?

>>otherwise if they had been pre increment would the value of the expression 'a1+b+c' be any different
No they are the same. pre and post increment only has any real difference if used in an expression of some sort. For example

int a = 1;

foo(++a);
foo(a++);

In the code above the value of a inside function foo() will be different because in the first instance the value of a is first incrmeneted then that value is passed to foo(). In the second instance the current value of a is passed to foo() then incremented after foo() returns to its caller.

>>This goes the same for a "For Loop"?
Yes, both these have the same identical result.

int i;
for(i = 0; i < 10; i++)
and 
for(i = 0; i < 10; ++i)

These two, however, are different

int i;
for(i = 0; i++ < 10;)
and 
for(i = 0; ++i < 10;)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.