Questions tagged [sequence-points]

Points in a program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation. (from Wikipedia)

This tag should be used whenever the question is related to problems that are (eventually found to be) caused by ambiguous execution of subexpressions, which results in the difference of the expected result by novice programmers.

189 questions
0
votes
2 answers

Does this C code result in Undefined Behavior?

I know that: int b = 1, c = 2, d = 3, e = 4; printf("%d %d %d", ++b, b, b++); results in undefined behavior. Since Modifying any object more than once between two sequence points is UB. Undefined behavior and sequence points But I don't know…
0
votes
0 answers

Is (++x > ++x) well-defined in c++?

I read a bit about undefined behavior regarding side-effects and sequence points and their replacement in C++11. (Undefined behavior and sequence points) It raised the following question. Does this code have defined behavior? int x = 0; bool result…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
3 answers

C Operators and Precedence

I'm using C language, for the below code: #include int main(int argc, char const *argv[]) { int num1=0; int res = ++num1 && num1++; printf("%d\t%d\n",num1,res); } In the above code I get output as 2 1. I think the…
0
votes
0 answers

Is binary operator '+' a sequence point in C?

I have the following code: #include int main() { int a = 2, c; c = a++ + a; printf("%d\n", c); // prints: 5 } I thought this would print 4 because the side effect of ++ shouldn't evaluate until the sequence point ;. However,…
PajLe
  • 791
  • 1
  • 7
  • 21
0
votes
1 answer

Why, the output is 6 and not 7?

The following C code executes correctly but not as expected. Post increment operator here in z=z++ is creating confusion here. I may not be able to figure out silly mistake/concept, Can I have a brief explanation or some helpful link please. …
skyconfusion
  • 123
  • 8
0
votes
0 answers

At a sequence point all previous accesses to volatile objects have stabilized

From GNU document about volatile: The minimum requirement is that at a sequence point all previous accesses to volatile objects have stabilized and no subsequent accesses have occurred Ok, so we know what sequence points are, and we now know…
izac89
  • 3,790
  • 7
  • 30
  • 46
0
votes
1 answer

Why are there no sequence point rule violations in this code?

Compiler used: gcc 8.2 Command line: -Wall My current understanding of a sequence point violation is code that somehow depends on the order of evaluation of operands/sub-expressions in a given expression. This is so because the order of evaluation…
0
votes
2 answers

Post Increment with respect to Sequence Points

When does the post increment operator affect the increment? I have come across two opinions: 1) From http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm: POST means do the operation after any assignment operation. 2) Closer home, an…
user191776
0
votes
1 answer

Sequence points in a language with left to right evaluation order?

When evaluation order is specified as "left to right" and language is a (pseudo) C-like, which are the sequence points in the following examples? int x = 1; int z = x-- + x; // z = 1 + 0 or z = 1 + 1? my_func(x++); // x incremented before or after…
gremo
  • 47,186
  • 75
  • 257
  • 421
0
votes
1 answer

Why printf ("%d%d%d", ++i, i, i++) is undefined behavior?

ISO/IEC 9899(TC2) §6.5 — 2 Expressions tells us: 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 read only to…
dhein
  • 6,431
  • 4
  • 42
  • 74
0
votes
1 answer

Sscanf uninitialized value when using prior argument in address

I am trying to use sscanf (on a string that I know is well formed and not malicious) to write a value to a specific part of an array. #include int main(void){ int arr[10], i; sscanf("5 5", "%d %d", &i, arr + i); } When I run this…
0
votes
2 answers

Order of Evaluation for passing function arguments - Order of operations for F1( int F2( int& x ), int x )

Okay so I was writing some code earlier. This was the line specifically: EnterNode( FindNode( terrain_X, terrain_Y, travel_dir ), travel_dir ); I noticed after testing my program something weird was happening. The value being received by the outer…
Josh C
  • 1,035
  • 2
  • 14
  • 27
0
votes
1 answer

C operator += Sequence point?

Is this defined behaviour? *p += *p--; And, if it is, is it equivalent to { p[0] += p[0]; --p; } or to { p[-1] = p[0]; --p; } ? I'm guessing the being defined or not depends on whether += has an implicit sequence point and, if it has, my guess is…
0
votes
0 answers

What makes C standard so difficult to determine the sequence point?

According to me knowledge with pre and post increment in C. A post-increment will take effect after the statement. Example: int num = 10; int sum = num ++; //sum & num at this point is 10 printf("%d %d", sum, num); //we should get 10 11 While…
user3437460
  • 17,253
  • 15
  • 58
  • 106
0
votes
2 answers

Reason for infinite loop

signed char ch=5; while(ch = ch--) printf("%d",ch); I read this. It is clearly stated that while statement and end of a statement(;) are sequence points. So i don't understand why the above one runs infinite time and prints same value[5].
Gibbs
  • 21,904
  • 13
  • 74
  • 138
1 2 3
12
13