473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sequence Point before actual function call

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.
Oct 27 '08 #1
8 1905


co**************@gmail.com wrote:
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,
--
Walter Banks
Byte Craft Limited
http://www.bytecraft.com

Oct 27 '08 #2
co**************@gmail.com wrote:
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" (if there is any, since the final line of output has no
newline character).
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 expression `j++' increases the value of `j', and yields
the value `j' had before it was incremented. As of the sequence
point that precedes f(), `j' has the value 1 (the increment has
taken place), and `x' has the value 0 (the value yielded by `j++').

Keep in mind that f() has no connection with `j' at all, but
only with the value of its argument expression as stored in `x'.

--
Er*********@sun.com
Oct 27 '08 #3
On Mon, 27 Oct 2008 11:12:37 -0700, coolguyaroundyou wrote:
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.
Oct 27 '08 #4
co**************@gmail.com writes:
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++);
}

Oct 27 '08 #5
co**************@gmail.com said:
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.
func()'s argument expression is j++. Since func() can't be called until the
argument expression has been evaluated, j++ must be evaluated. Now, j++
evaluates to 0, so 0 is the argument expression's value, so that's what
func() will see. There is a side effect (the value of j changes), but that
doesn't effect the result of the expression j++. It's still 0, because
post-increment ++ yields as its value the value that the operand had
before ++ got its hands on it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 27 '08 #6
co**************@gmail.com wrote:
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
Oct 27 '08 #7
Walter Banks <wa****@bytecraft.comwrites:
co**************@gmail.com wrote:
>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.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 27 '08 #8
Walter Banks wrote, On 27/10/08 18:26:
>
co**************@gmail.com wrote:
>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.
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.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
7
by: akarl | last post by:
Hi all, Why do I get a warning from gcc with the following program? $ cat test.c #include <stdio.h> int f(int n) { return n;
17
by: Frederick Gotham | last post by:
I know there's a sequence point at a comma, e.g.: int main(void) { int a = 1; a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
15
by: Frederick Gotham | last post by:
Here's a sample function which converts a string to all uppercase: #include <assert.h> #include <ctype.h> void StringUp( char *p ) { do assert( *p >= 0 ); while( *p = toupper( *p ), *p++ );...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
2
by: Spoon | last post by:
Hello, Is the following code valid: char buf; int size = 666; size += sprintf(buf+size, "%d", size); size += sprintf(buf+size, "%d", size); (Expected behavior: at offset 666, buf contains...
4
by: Tomás Ó hÉilidhe | last post by:
I'm writing a program currently that was working perfectly until I decided to compile it with "-O3" in gcc (-O3 specifies optimisation of the third level). Anyway, I found the problem. I had the...
3
by: joe | last post by:
Consider the following program: include <iostream> class Bar { public: int getData9() { m_data = 9; return m_data;} int getData11() { m_data = 11; return m_data;} int m_data;
21
by: coolguyaroundyou | last post by:
See the below code: void func(int x) { printf("%d",x); } int main() { int j=0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.