Instructor: Stefan Mitsch
int f (int x) {
int y;
if (x) y=1; else y=2;
return y;
}
int main() { printf ("%d\n", f(5)); return 0; }
int f (int x) {
int y;
y = if (x) 1 else 2;
return y;
}
int main() { printf ("%d\n", f(5)); return 0; }
int f (int x) {
int y;
y = x ? 1 : 2;
return y;
}
int main() { printf ("%d\n", f(5)); return 0; }
int f (int x) {
int y;
y = {int z=0; while (x>0) {x--; z++;} z}
return y;
}
int main() { printf ("%d\n", f(5)); return 0; }
f (1 + (2 * strlen ("hello")))
printf("hello");
^^^^^^^^^^^^^^^ expression
^^^^^^^^^^^^^^^^ statement
return 1+x;
^^^ expression
^^^^^^^^^^^ statement
int count = 0;
while (1) {
int ch = getchar();
switch (ch) {
case -1: return count;
case 'a': count = count + 1;
default: continue;
}
}
x++
x += 2
x = (y = 5)
x -= (y += 5)
int x = 1;
printf ("%d\n", ++x); //
//
int x = 1;
printf ("%d\n", x++); //
//
x = 1 + (y = 5); //
int x = 1;
printf ("%d\n", (x = x + 1) + x); //
int x = 1;
printf ("%d\n", ++x); // pre increment, prints 2
// value of x is now 2
int x = 1;
printf ("%d\n", x++); //
//
x = 1 + (y = 5); //
int x = 1;
printf ("%d\n", (x = x + 1) + x); //
int x = 1;
printf ("%d\n", ++x); // pre increment, prints 2
// value of x is now 2
int x = 1;
printf ("%d\n", x++); // post increment, prints 1
// value of x is now 2
x = 1 + (y = 5); //
int x = 1;
printf ("%d\n", (x = x + 1) + x); //
int x = 1;
printf ("%d\n", ++x); // pre increment, prints 2
// value of x is now 2
int x = 1;
printf ("%d\n", x++); // post increment, prints 1
// value of x is now 2
x = 1 + (y = 5); // assigns 5 to y and 6 to x
int x = 1;
printf ("%d\n", (x = x + 1) + x); //
int x = 1;
printf ("%d\n", ++x); // pre increment, prints 2
// value of x is now 2
int x = 1;
printf ("%d\n", x++); // post increment, prints 1
// value of x is now 2
x = 1 + (y = 5); // assigns 5 to y and 6 to x
int x = 1;
printf ("%d\n", (x = x + 1) + x); // no "sequence point", undefined!
int global = 0;
int post_inc () {
return global++;
}
int main () {
printf ("%d\n", post_inc () + post_inc ());
}
(e1, e2, ..., en)
e1 ... en-1 executed for side effect
en
string s;
while(read_string(s), s.len() > 5) {
// do something
}
int main () {
int x = 5;
x *= 2;
printf ("%d\n", x);
}
int main () {
int x = 5;
printf ("%d\n", (x *= 2, x));
// behavior defined because comma operator introduces sequence point
}
e1, e2
e1 ? e2 : e3