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

Dani AI

Generated

Short answer to : with the code shown, b and c are local and reinitialized to 0 on every call. The two ++ statements run before the return, so the function returns a1 + 1 + 1. If a is 2 each call prints 4 (so the output will be 4 4 4). 's point about pre- vs post-increment is correct: when the increment is its own statement there is no difference in the final numeric result.

Useful clarifications and gotchas:

  • If the increments and the sum happen in the same expression, pre- and post-increment change what value contributes to the expression. For example, with persistent state (static variables) you will see different sequences depending on b++/c++ versus ++b/++c:
int f_post(int a) {
    static int b = 0, c = 0;
    return a + b++ + c++;   // first call uses old b,c, then increments
}

int f_pre(int a) {
    static int b = 0, c = 0;
    return a + ++b + ++c;   // first call increments then uses the new values
}
  • For loops: the usual for (i = 0; i < n; i++) and for (i = 0; i < n; ++i) behave identically for built-in integers. However, for user-defined iterators (containers, custom iterator types) prefer pre-increment (++it) because post-increment usually constructs a temporary copy and can be less efficient.

  • Never rely on modifying the same scalar object more than once in a single unsequenced expression — that causes undefined behavior. For authoritative details on increment semantics and expression evaluation rules see the C++ reference pages for operator++ and evaluation order: operator++ and evaluation order.

This addresses the immediate output question and gives practical variants to try (persistent state, increments inside the expression, iterators) so the differences become obvious.

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.