Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence points in C / function calls

Tags:

c

I'm just learning some C, or rather, getting a sense of some of the arcane details. And I was using VTC advanced C programming in which I found that the sequence points are :

  • Semicolon
  • Comma
  • Logical OR / AND
  • Ternary operator
  • Function calls (Any expression used as an argument to a function call is finalized the call is made)

are all these correct ?. Regarding the last one I tried:

void foo (int bar) { printf("I received %d\n", bar); }

int main(void) 
{

  int i = 0;

  foo(i++);

  return 0;

}

And it didnt print 1, which according to what the VTC's guy said and if I undertood correctly, it should, right ?. Also, are these parens in the function call the same as the grouping parens ? (I mean, their precedence). Maybe it is because parens have higher precedence than ++ but I've also tried foo((i++)); and got the same result. Only doing foo(i = i + 1); yielded 1.

Thank you in advance. Please consider that I'm from South America so if I wasnt clear or nothing makes sense please, oh please, tell me.

Warmest regards, Sebastian.

like image 504
SJPRO Avatar asked Jul 04 '13 06:07

SJPRO


People also ask

What does it call when the points are written in sequence?

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.

Where did the next sequence point arises?

Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the C expression i=i++ , which apparently both assigns i its previous value and increments i .

How do you evaluate an expression in C?

In the C programming language, an expression is evaluated based on the operator precedence and associativity. When there are multiple operators in an expression, they are evaluated according to their precedence and associativity.

What is the effect of C?

In some people, vitamin C might cause side effects such as stomach cramps, nausea, heartburn, and headache. The chance of getting these side effects increases with higher doses. Taking more than 2000 mg daily is possibly unsafe and may cause kidney stones and severe diarrhea.


1 Answers

Your code is working like it should and it has nothing to do with sequence points. The point is that the postfix ++ operator returns the non-incremented value (but increments the variable by 1 nonetheless).

Basically:

  • i++ – Increment i by one and return the previous value
  • ++i – Increment i by one and return the value after the increment

The position of the operator gives a slight hint for its semantics.

like image 147
Joey Avatar answered Oct 21 '22 22:10

Joey