cwalkthrough

Walk-through of a simple c program


I have this question to walk through and show the output when this program is run. The one thing I don't understand is how f is found to be four or even found at all.I know the correct answer is

7 falcon 3

9 RK 4

_
I just dont know how they found the f value to be 4 ,once i have that I can do the rest fine

#include <stdio.h> 
#include <string.h> 

void falcon(int f); 
char a[20]; 

int main() { 
    int i, j; 
    a[3] = 'G'; 
    a[1] = 'K'; 
    i = 3 + 2 * 3; 
    j = 4; 
    a[2] = 'Y'; 
    falcon(j); 
    printf("%d %s %d\n", i, a, j); 
} 

void falcon(int f) { 
    int j; 
    j = 11 % f; 
    printf("%d falcon %d\n", f+3, j); 
    a[2] = '\0'; 
    a[0] = 'R'; 
}

Solution

  • Let's walk through the program together (with some of the irrelevant bits cut out).

    #include <stdio.h> 
    #include <string.h> 
    
    void falcon(int f); 
    char a[20]; 
    
    int main() {
        int i, j;
        j = 4; 
        falcon(j); // in other words, falcon(4). Now, let's go down to the 
        // falcon function where the first argument is 4.
        printf("%d %s %d\n", i, a, j); 
    } 
    
    void falcon(int f) { // Except here we see that in this function,
                         // the first argument is referred to by 'f', 
                         // which, as we saw, is 4.
        int j; 
        j = 11 % f; // here, j is equal to the remainder of 11 divided by
                    // f, which is 4.
        printf("%d falcon %d\n", f+3, j);
    }