0
 m=3;
function(m++);

Which parameter will be passed 3 or 4? I have read that in post increment value changes after statement is completed (semi colon is reached) but there are no semicolon so will the value don't change and remain 3?

5 Answers5

2

The statement

function(m++);

is essentially equivalent to

{
    type_of_m compiler_generated_temporary_variable = m;
    m = m + 1;
    function(compiler_generated_temporary_variable);  // Passes the old value of m
}

So m will be increased once the full expression is finished, but its old value will be passed to function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Is this set by the standard or is it compiler-dependent? – RobertS supports Monica Cellio Mar 27 '20 at 18:19
  • 3
    @RobertSsupportsMonicaCellio: [6.5.2.2/10](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) - "There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call." So the side effect to `m` will be applied before the function is actually called. – John Bode Mar 27 '20 at 22:18
2

The expression m++ has a result and a side effect.

The result of m++ is the current value of m (3) - that's what gets passed as the argument to the function. The side effect is that 1 gets added to m.

It's roughly equivalent to writing

function( m );
m += 1;

except that there is sequence point between the evaluation of the function's arguments and the actual function call, by which time all the side effects in the argument list must be applied, so m will be updated before the function executes.

Similarly, the result of the expression ++m is the value of m plus 1, and the side effect is that 1 gets added to m, so if you passed ++m as the function argument, the function would receive the value 4.

Again, it's roughly equivalent to

function( m + 1 );
m += 1;

with the same caveat as above regarding when m is updated.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

Which parameter will be passed 3 or 4?

The C standard primarily says two things about m++:

  • The value of this expression is the value of m before any increment is performed.
  • As a side effect, the stored value of m is incremented by 1.

I have read that in post increment value changes after statement is completed (semi colon is reached)

That is incorrect. The C standard is not specific about when the stored value changes. It could change before the value of this expression is used (but the value used will still be the prior value of m, even if the compiler has to store that value in some temporary place to remember it), it could change after the value is used, or it could even change during uses of the value.

The C standard does have some rules about this. One is that all side effects are complete before the next sequence point. Another is that there is a sequence point after the evaluation of every full expression, which includes the whole expression in an expression statement. (An expression statement is an expression followed by a semicolon.) Therefore, the increment of m will occur before the statement is complete, not after and not necessarily just before it completes, but it may occur at various times during the statement.

Another rule the C standard has about side effects is that there is a sequence point after evaluation of the arguments to a function call and before the actual call. So, in m=3; function(m++);, the value passed is 3, m is changed to 4, and the function is called, and, furthermore, the change of m to 4 must be complete before function is called.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

This is post-increment, meaning it will increment AFTER that expression has finished.

m++

This is pre-increment, meaning it will increment BEFORE that expression has been executed.

++m

So in your use case, it will pass 3, NOT 4.

fables.fab
  • 43
  • 3
  • But isn't value is passed after the statement is executed, and due to execution of statement, value is increased by 1, where I am wrong in here, is the value passed before the execution of statement? – Piesquareisg Mar 27 '20 at 18:06
  • @Randomuser “But isn't value is passed after the statement is executed” — how would this work? In order for the statement to be executed the value has to be first passed. That’s what “executing the statement” entails! – Konrad Rudolph Mar 27 '20 at 18:44
  • @KonradRudolph correct. Perhaps I was a bit unclear when I explained it above, but I mean exactly to say that when you pass it to your function, it will work with the original assigned 'm' value if you pass it with 'post-increment'. If you pass it with 'pre-increment', your function will see m+1 as the parameter you pass into it. – fables.fab Mar 27 '20 at 18:53
0

Let's look at the instructions generated by GCC for the following piece of code

int main(void) {

    int x;

    x = 5;

    func(x++);

    return(0);
}

-

main:
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $16, %rsp
        movl    $5, -4(%rbp)       ; 5 gets stored
        movl    -4(%rbp), %eax     ; moves the value 5 into %eax register
        leal    1(%rax), %edx      ; %edx contains the value 6
        movl    %edx, -4(%rbp)     ; moves the value 6 into x
        movl    %eax, %edi         ; moves the value of %eax, which is 5, into %edi for the function to use
        call    func
        movl    $0, %eax
        leave
        ret

Note, that the value 6 and the value 5 reside in different registers.

Vio Ariton
  • 407
  • 1
  • 5
  • 17
  • 1
    Looking at the instructions generated tells you only what one compiler did in one circumstances. It does not tell you what the rules of the language standard are. – Eric Postpischil Mar 27 '20 at 20:20