#include <stdio.h> // for printf()
// function call precedence
int f(int *x);
int g(int *x);
int main()
{
int x = 0;
x = f(&x) + g(&x);
printf("x = %d\n", x);
return 0;
}
int f(int *x)
{
printf("f(): x = %d\n", *x);
(*x)++;
printf("f(): x = %d\n", *x);
return *x;
}
int g(int *x)
{
printf("g(): x = %d\n", *x);
(*x)--;
printf("g(): x = %d\n", *x);
return *x;
}
/*
gcc funp.c -o funp
./funp
f(): x = 0 // initial value
f(): x = 1 // after incrementation
g(): x = 1 // after f()
g(): x = 0 // after decrementation
x = 1 // 1 + 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// arguments evaluation precedence
int main()
{
int i = 0; // initialize
printf("i = %d\n++i = %d\ni = %d\n\n", i, ++i, i);
i = 0; // reset
printf("i = %d\ni++ = %d\ni = %d\n\n", i, i++, i);
i = 0; // reset
printf("i = %d\n++i = %d\ni++ = %d\ni = %d\n\n", i, ++i, i++, i);
i = 0; // reset
printf("i = %d\ni++ = %d\n++i = %d\ni = %d\n", i, i++, ++i, i);
return 0;
}
/*
gcc argp.c -o argp
./argp
i = 1 // before incrementation, holds final value
++i = 1 // final value, after incrementation
i = 1 // after incrementation, holds final value
i = 1 // before incrementation, holds final value
i++ = 0 // holds initial value
i = 1 // after incrementation, holds final value
i = 2 // before any incrementation, holds final value
++i = 2 // final value, after both incrementations
i++ = 0 // holds initial value
i = 2 // after incrementations, holds final value
i = 2 // before any incrementation, holds final value
i++ = 1 // value after one incrementation
++i = 2 // final value, after both incrementations
i = 2 // after incrementations, holds final value
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), putchar()
// array precedence
int main()
{
int i, a[10];
for (i = 0; i < 10; i++)
{a[i] = -10;} // initialize a[]
i = -1;
while (i < 9)
{
a[i] = ++i;
}
printf("a[i] = ++i, i = -1..8\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -1;
while (i < 9)
{
a[i] = i++;
}
printf("a[i] = i++, i = -1..8\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -1;
while (i < 9)
{
a[++i] = i;
}
printf("a[++i] = i, i = -1..8\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = 0;
while (i < 10)
{
a[i++] = i;
}
printf("a[i++] = i, i = 0..9\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -2;
while (i < 8)
{
a[++i] = ++i, a[i+1] = i+1; // test value of i
}
printf("a[++i] = ++i, a[i+1] = i+1, i = -2..6 (step 2)\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -2;
while (i < 8)
{
a[++i] = i++, a[i+1] = i+1; // test value of i
}
printf("a[++i] = i++, a[i+1] = i+1, i = -2..6 (step 2)\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -1;
while (i < 9)
{
a[i++] = ++i, a[i] = i+1; // test value of i
}
printf("a[i++] = ++i, a[i] = i+1, i = -1..7 (step 2)\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
putchar('\n');
for (i = 0; i < 10; i++)
{a[i] = -10;} // reset a[]
i = -1;
while (i < 9)
{
a[i++] = i++, a[i] = i+1; // test value of i
}
printf("a[i++] = i++, a[i] = i+1, i = -1..7 (step 2)\n");
for (i = 0; i < 10; i++)
{printf("a[%d] = %d,%c", i, a[i], (i % 5 == 4) ? '\n' : ' ');}
return 0;
}
/*
gcc arrp.c -o arrp
./arrp
a[i] = ++i, i = -1..8
a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4,
a[5] = 5, a[6] = 6, a[7] = 7, a[8] = 8, a[9] = 9,
// ++i is executed before the assignment and before the subscript is chosen
a[i] = i++, i = -1..8
a[0] = -1, a[1] = 0, a[2] = 1, a[3] = 2, a[4] = 3,
a[5] = 4, a[6] = 5, a[7] = 6, a[8] = 7, a[9] = 8,
// i++ is executed after the assignment, but before the subscript is chosen
a[++i] = i, i = -1..8
a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4,
a[5] = 5, a[6] = 6, a[7] = 7, a[8] = 8, a[9] = 9,
// ++i is executed before the subscript is chosen and before the assignment
a[i++] = i, i = 0..9
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5,
a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 9, a[9] = 10,
// i++ is executed after the subscript is chosen, but before the assignment
a[++i] = ++i, a[i+1] = i+1, i = -2..6 (step 2)
a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4,
a[5] = 5, a[6] = 6, a[7] = 7, a[8] = 8, a[9] = 9,
// both ++i before both operations
a[++i] = i++, a[i+1] = i+1, i = -2..6 (step 2)
a[0] = -2, a[1] = 1, a[2] = 0, a[3] = 3, a[4] = 2,
a[5] = 5, a[6] = 4, a[7] = 7, a[8] = 6, a[9] = 9,
// ++i before subscript, before assignment (but after i++), so after assignment,
// i++ before subscript, after assignment
a[i++] = ++i, a[i] = i+1, i = -1..7 (step 2)
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5,
a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 9, a[9] = 10,
// i++ after subscript, before assignment, ++i before both operations
a[i++] = i++, a[i] = i+1, i = -1..7 (step 2)
a[0] = -1, a[1] = 2, a[2] = 1, a[3] = 4, a[4] = 3,
a[5] = 6, a[6] = 5, a[7] = 8, a[8] = 7, a[9] = 10,
// first i++ after subscript, before assignment (but after i++), so after assignment,
// second i++ before subscript, after assignment
*/
For one incrementation (the first 4 cases), i varies from -1 to 8 or from 0 to 9 (10 values), corresponding to a[0]..a[9].
For two incrementations (the last 4 cases) we need an extra assignment because i is increased by 2 at each step. Here i varies from -2 to 6 (step 2) or from -1 to 7 (step 2), 5 values in total, corresponding to a[0], a[2], a[4], a[6], a[8]. For the other 5 values, we use the extra assignment a[i+1] = i+1 or a[i] = i+1, depending on the value of i.
After each case, we reset a[] to values -10 to be sure the next case sets all the values of a[] (unset values will display -10).