/* * side_effects.c * CS 251 Lesson 6 * Studies in the side effects of C statements */ // Compile with: // clang -o side_effects side_effects.c // // Run with: // ./side_effects #include // GLOBALS!! int a; int b; void f(int x) { int a = 4; x += 1; printf("In f: %d %d\n", a, x); } void g() { a += 1; printf("In g: a=%d\n", a); } void h(char *vals) { vals[0] = 'X'; printf("In h: { %c %c %c %c }\n", vals[0], vals[1], vals[2], vals[3]); } int main() { // First we'll play with some integers a = 10; b = a; b = 20; printf("/** TEST 1 **/\n"); printf("%d, %d\n", a, b); // Now we'll look at function scope printf("\n/** TEST 2 **/\n"); printf("Before f: %d %d\n", a, b); f(b); printf("After f: %d %d\n", a, b); // How could we change a? printf("\n/** TEST 3 **/\n"); printf("Before g: %d %d\n", a, b); g(); printf("After g: %d %d\n", a, b); // Okay, now what if we look at lists, not just ints? (We'll use arrays.) printf("\n/** TEST 4 **/\n"); char lst[4] = { 'a', 'b', 'c', 'd' }; printf("Before h: { %c %c %c %c }\n", lst[0], lst[1], lst[2], lst[3]); h(lst); printf("After h: { %c %c %c %c }\n", lst[0], lst[1], lst[2], lst[3]); return 0; }