0
0
Cprogramming~20 mins

Linking multiple files in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Linking Multiple Files
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a program with two linked files

Given two C files linked together, what is the output when the program runs?

C
/* file1.c */
#include <stdio.h>

void greet();

int main() {
    greet();
    return 0;
}

/* file2.c */
#include <stdio.h>

void greet() {
    printf("Hello from file2!\n");
}
ARuntime error: segmentation fault
BCompilation error: undefined reference to greet
CHello from file1!
DHello from file2!
Attempts:
2 left
💡 Hint

Remember that functions declared in one file can be defined in another and linked together.

Predict Output
intermediate
2:00remaining
Value of a global variable shared across files

Consider two C files sharing a global variable. What is the value printed?

C
/* file1.c */
#include <stdio.h>

extern int counter;

int main() {
    counter = 5;
    print_counter();
    return 0;
}

/* file2.c */
#include <stdio.h>

int counter = 0;

void print_counter() {
    printf("Counter: %d\n", counter);
}
ACounter: 5
BCounter: 0
CCompilation error: multiple definitions of counter
DRuntime error: uninitialized variable
Attempts:
2 left
💡 Hint

Global variables declared with extern in one file and defined in another share the same memory.

🔧 Debug
advanced
2:00remaining
Identify the linking error in multiple files

Why does this program fail to link?

C
/* file1.c */
#include <stdio.h>

void print_message();

int main() {
    print_message();
    return 0;
}

/* file2.c */
#include <stdio.h>

static void print_message() {
    printf("Hello from file2!\n");
}
ACompilation error: static function cannot be declared
BRuntime error: segmentation fault
CLinker error: undefined reference to print_message
DProgram runs and prints 'Hello from file2!'
Attempts:
2 left
💡 Hint

Think about the meaning of static for functions in C files.

📝 Syntax
advanced
2:00remaining
Correct declaration for a function defined in another file

Which option correctly declares a function defined in another file to avoid linking errors?

C
/* file1.c */
#include <stdio.h>

// Function declaration here

int main() {
    display();
    return 0;
}

/* file2.c */
#include <stdio.h>

void display() {
    printf("Display function called\n");
}
Avoid display();
Bextern void display();
Cvoid display(void);
Dstatic void display();
Attempts:
2 left
💡 Hint

Consider how to tell the compiler the function exists elsewhere.

🚀 Application
expert
3:00remaining
Number of symbols visible after linking multiple files

Given three C files below, how many functions are visible to main() after linking?

C
/* file1.c */
#include <stdio.h>

void funcA();

int main() {
    funcA();
    funcB();
    funcC();
    return 0;
}

/* file2.c */
#include <stdio.h>

void funcA() {
    printf("Function A\n");
}

static void funcB() {
    printf("Function B\n");
}

/* file3.c */
#include <stdio.h>

void funcC() {
    printf("Function C\n");
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Remember that static functions are only visible inside their own file.