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
3 answers

Microsoft C deviation from standard

Any reason for the following aberration? Consider the following C program (named PstFixInc.c) #include int main (int argc, char *argv []) { int num = 0; num = (num++) % 4; printf ("num: %d\n",num); return 0; } If compiled with gcc…
0
votes
3 answers

Swapping the values of two variables without using third variable in C?

I've found the following code snippet: #include int main(void) { int x=10,y=15; x=x+y-(y=x); printf("x=%d y=%d",x,y); return 0; } It actually swaps the variables Can anybody explain me how the the code swaps the…
TryinHard
  • 4,078
  • 3
  • 28
  • 54
0
votes
4 answers

Why doesn't comma operator seem to work between a "if" statement and an "else" statement in my code?

I know a statement like the following (commas in place of semi-colons) looks odd: if(a
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
0
votes
3 answers

Confusing answers : One says *myptr++ increments pointer first,other says *p++ dereferences old pointer value

I would appreciate if you clarify this for me.Here are two recent questions with their accepted answers: 1) What is the difference between *myptr++ and *(myptr++) in C 2) Yet another sequence point query: how does *p++ = getchar() work? The accepted…
-1
votes
1 answer

Strange behavior with post and pre increment operators in C

When I do this - int i = 4; printf("\n %d", ++i + ++i); I get 12 as the answer. But when I do this - int i = 4; int a,b,s; a = ++i; b= ++i; s = a+b; printf("%d", s); I get 11 as the answer. Why? I have tried both the code. The expected value is 11…
-1
votes
4 answers

C Increment Operator Explanation

On executing this piece of C command, the output of num is 7. I was expecting it to be 6, can anyone explain why and how it turns out to be 7? #include int main() { int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int i = 0, num =…
-2
votes
3 answers

Explaination for printf with comparing variables as arguments

main(){ int a = 5; int b = 6; printf("%d %d %d",a==b,a=b,a
-4
votes
1 answer

Beginner's query about C program Function Call stack, sequence point(sequencing)

The code below displays different results when compiled and run on Code::Blocks. void sum(int a,int b){ printf("a=%d b=%d\n",a,b); } int main(){ int i=1; sum(i=5,++i); printf("i=%d\n\n",i); /***********************/ i=2; …
-4
votes
1 answer

Printing order in C

int x=10; printf("%d %d %d\n",x,++x,x++); printf("%d %d %d",x,x+20,x+30); It is printing output as 12 12 10 12 32 42 Why the order in first printf is in reverse order and why not in second printf statement? i found in a book that C uses reverse…
Rakesh
  • 133
  • 1
  • 8
1 2 3
12
13