Sequence Point before actual function call

C

coolguyaroundyou

See the below code:

void func(int x)
{
printf("%d",x);
}

int main()
{
int j=0;
func(j++);
return 0;
}

What should be the output?

The C Standard says that there is a sequence point before the actual
function call. So, according to me it should have been 1 but I got
0(zero) on my screen.
 
W

Walter Banks

See the below code:

void func(int x)
{
printf("%d",x);
}

int main()
{
int j=0;
func(j++);
return 0;
}

What should be the output?

The C Standard says that there is a sequence point before the actual
function call. So, according to me it should have been 1 but I got
0(zero) on my screen.

The function is called before the increment.


func(++j);

The result should be 1. (Preincrement)


Regards,
 
V

viza

int j=0;
func(j++);

The C Standard says that there is a sequence point before the actual
function call. So, according to me it should have been 1 but I got
0(zero) on my screen.

That would be func(++j) (pre-increment). You used a post-increment.
 
N

Nate Eldredge

See the below code:

void func(int x)
{
printf("%d",x);
}

int main()
{
int j=0;
func(j++);
return 0;
}

What should be the output?
0.

The C Standard says that there is a sequence point before the actual
function call. So, according to me it should have been 1 but I got
0(zero) on my screen.

That would mean that the value of the variable j should be 1 before the
function is called, but the value of the expression that you passed to
func is still 0.

The following two examples might be instructive:

void foo(void) {
int j = 0;
int k;
k = j++;
func(k);
}

/* and: */

int q;
void func2(int x) {
printf("x = %d, q = %d\n", x, q);

void bar(void) {
q = 0;
func2(q++);
}
 
M

Martin Ambuhl

See the below code:

void func(int x)
{
printf("%d",x);
}

int main()
{
int j=0;
func(j++);
return 0;
}

What should be the output?

The C Standard says that there is a sequence point before the actual
function call.

So what?
So, according to me it should have been 1 but I got
0(zero) on my screen.

Well, "according to [you]" seems to be a very non-authoritative source:

#include <stdio.h> /* mha: added. printf is a variadic
function and needs a declaration */
void func(int x)
{
printf("%d\n", x); /* mha: added '\n'. Without this you
relation on implementation defined
behavior */
}

int main()
{
int j = 0;
printf("[Output]\n");
func(j++);
/* mha: added code so you can learn the difference between j++ and
++j */
j = 0;
printf("[Added output]\nThe value of j is %d\n", j);
printf("j++ should have the value of j _before_ the increment,\n"
" it is actually %d\n", j++);
printf("The new value of j is %d\n\n", j);
j = 0;
printf("The value of j is %d\n", j);
printf("++j should have the value of j _after_ the increment,\n"
" it is actually %d\n", ++j);
printf("The new value of j is %d\n\n", j);
/* mha: end of added code */
return 0;
}

[Output]
0
[Added output]
The value of j is 0
j++ should have the value of j _before_ the increment,
it is actually 0
The new value of j is 1

The value of j is 0
++j should have the value of j _after_ the increment,
it is actually 1
The new value of j is 1
 
K

Keith Thompson

Walter Banks said:
The function is called before the increment.

No, the increment occurs before the function is called, and there's a
sequence point between those two actions. But the expression ``j++''
yields the value that j has *before* the increment. If func examines
the value of j (either because j has been moved to file scope or
because it can see it via a pointer), it will see the value 1, but the
value of the parameter x is 0.
 
F

Flash Gordon

Walter Banks wrote, On 27/10/08 18:26:
The function is called before the increment.

No, it is called after the increment as all side-effects (and the
increment is a side effect) have to be completed before the sequence
point. It is just that the value passed to the function is the
pre-increment value.
func(++j);

The result should be 1. (Preincrement)

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top