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