473,657 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1941


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, coolguyaroundyo u 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****@bytecra ft.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_Keit h) 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.cause way.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
4063
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 object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
7
2071
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
1911
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
1646
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
2502
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 uses errno, modified by right operand): (log(x) - mu) * (log(x) - mu) Code has unspecified behavior. Order of evaluation of function parameters or subexpressions is not defined, so if a value is used and
2
1286
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 "666669\0".)
4
2528
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 following function: void StrToLower(char *p) { while ( *p++ = tolower( (char unsigned)*p ) ); }
3
1679
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
479
by: coolguyaroundyou | last post by:
See the below code: void func(int x) { printf("%d",x); } int main() { int j=0;
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.