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?
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?
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
.
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.
Which parameter will be passed 3 or 4?
The C standard primarily says two things about m++
:
m
before any increment is performed.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.
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.
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.