473,796 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is sequence point ?

Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.
Nov 14 '05 #1
3 7109
sugaray <ru****@sohu.co m> scribbled the following:
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.


A sequence point is a point of time in the evaluation of a C expression,
where all side effects are guaranteed to have taken place.
Modifying the same variable twice, or modifying a variable and using its
old value for anything other than computing the new value, without a
sequence point in between, causes undefine behaviour.
If I remember correctly, sequence points include:
- The , (comma) and ?: (conditional) operators,
- The && and || operators,
- The semicolon at the end of a statement, and
- The entry point to a function, but this only concerns expressions
within that function.
Most importantly, the assignment operators (such as =), the pre-
and post- increment and decrement operators (++ and --), and the
comma *separator* in parameter lists are NOT sequence points.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
Nov 14 '05 #2
ru****@sohu.com (sugaray) writes:
Can somebody explain to me what is sequence point ?


Read the FAQ, please. (Q3.8)

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
ru****@sohu.com (sugaray) wrote in message news:<ad******* *************** ****@posting.go ogle.com>...
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.


Joona has already explained the sequence points and its basic workings.
However, one thing he missed is the test condition of the while loop.
For a better cover of the sequence points, side effects and full expressions,
you need to consult a good book and/or C-FAQs.

As you asked for examples, here is one including a while loop sequence point.

#include <stdio.h>
int main(void)
{
int i;

for(i = 1; i <= 10; i++)
printf("%d\n", i);
printf("\n");

i = 1;
while(i++ <= 10)
printf("%d\n", i);
printf("\n");

return 0;
}

As you can see, in the for construct i is initialized to 1, then tested with
the value 10 and finally incremented after executing the printf() statement.
I don't think there is a need to explain the workings of the for loop, is it?
It normally prints the value of i from 1 to 10 on your output device, along
with newlines.

Then, we reset i to 1.
Notice that the while loop can be made to imitate the for loop, quite easily.
It appears that i will get incremented only after the printf() has done it job.
This way, the while loop should also print the values from 1 to 10, after all
this is what post-increment operator does, isn't it?

Not quite so. The while loop test condition serves as a sequence point. So,
in this case, i gets incremented immediately before the printf() receives
control. Side effect of this condition test is that i will get incremented
before the program reaches printf() statement. In the end, values from 2 to
11 will be printed on your output device.

But, what if you change the postfix form of i to prefix form? It still won't
help too much. It will only stop incrementing i after comparing it with 10,
thus resulting in printing of values 2 to 10.

HTH,
Manish Singh
Nov 14 '05 #4

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

Similar topics

3
2390
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
53
4100
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
2083
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
1923
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
1655
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++ ); }
17
2117
by: Grizlyk | last post by:
Hello. What can be optimised in C++ code and how i can garantee stable behaviour below 1. Are expression "auto volatile" can deny removing as "unused temporary" like this: auto volatile const class_name tmp;
2
1641
by: ais523 | last post by:
The program excerpt int i; char c; char* a= {"abc","def","ghi"}; /* ... */ i=0; c=a; obviously invokes undefined behaviour, because i is modified twice
3
2480
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a function, after the arguments have been evaluated (6.5.2.2). -- The end of the first operand of the following operators: logical AND && (6.5.13);
4
2535
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 ) ); }
0
9683
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
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9054
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
7550
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
5443
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...
1
4119
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
3
2927
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.