📖 Scenario: You are creating a simple C program split into two files: one file will contain a function to greet a user, and the other will call this function and print the greeting.
🎯 Goal: Build a C program with two files: greet.c containing a function greet that returns a greeting string, and main.c that calls greet and prints the greeting.
📋 What You'll Learn
Create a function greet in greet.c that returns a greeting string.
Declare the function greet in a header file greet.h.
Include greet.h in main.c and call greet.
Print the greeting returned by greet in main.c.
Compile and link both files to produce the final executable.
💡 Why This Matters
🌍 Real World
Large C programs are split into multiple files to organize code better and allow multiple programmers to work together.
💼 Career
Understanding how to link multiple files is essential for software development in C, especially in systems programming and embedded development.
Progress0 / 4 steps
1
Create the greeting function in greet.c
Create a file called greet.c and write a function called greet that returns the string "Hello from greet.c!". Use const char* as the return type.
C
Hint
Define a function named greet that returns a constant string.
2
Create the header file greet.h
Create a header file called greet.h that declares the function greet with the signature const char* greet();. Add include guards to prevent multiple inclusion.
C
Hint
Use #ifndef, #define, and #endif to create include guards.
3
Write main.c to call greet
Create a file called main.c. Include stdio.h and greet.h. In main, call greet and store the result in a variable called message. Then, print message using printf.
C
Hint
Include the header greet.h and use printf to display the greeting.
4
Compile and run the program
Compile greet.c and main.c together using a command like gcc main.c greet.c -o greet_program. Then run the program with ./greet_program and print the output.
C
Hint
Use the terminal to compile and run. The output should be exactly Hello from greet.c!
Practice
(1/5)
1. What is the main purpose of linking multiple files in a C program?
easy
A. To organize code into separate files and combine them into one program
B. To run multiple programs at the same time
C. To convert C code into machine language
D. To write comments in different files
Solution
Step 1: Understand code organization
Linking multiple files allows splitting code into parts for better organization and reuse.
Step 2: Understand linking process
After compiling separately, linking combines these parts into one executable program.
Final Answer:
To organize code into separate files and combine them into one program -> Option A
Quick Check:
Linking = Combining files [OK]
Hint: Linking combines separate code files into one program [OK]
Common Mistakes:
Confusing linking with running multiple programs
Thinking linking converts code to machine language
Believing linking is for writing comments
2. Which command correctly compiles two C files main.c and utils.c together using GCC?
easy
A. gcc main.c -o program
B. gcc -o program -c main.c utils.c
C. gcc main.c utils.c -o program
D. gcc program -o main.c utils.c
Solution
Step 1: Understand GCC command structure
The correct syntax is: gcc [source files] -o [output file].
Step 2: Check options order
The standard GCC syntax is gcc [source files] -o [output], as in gcc main.c utils.c -o program.
When compiling separately and linking, it gives an error. Why?
medium
A. Cannot compile multiple files separately
B. Missing #include in file1.c
C. main() should be in file1.c
D. greet() is not declared before use in file2.c
Solution
Step 1: Check function declaration in file2.c
file2.c calls greet() but does not declare it or include a header with its prototype.
Step 2: Understand compiler error
Without declaration, compiler errors occur because it doesn't know greet() signature.
Final Answer:
greet() is not declared before use in file2.c -> Option D
Quick Check:
Declare functions before use [OK]
Hint: Always declare functions before calling them [OK]
Common Mistakes:
Assuming definition is enough without declaration
Ignoring missing #include (not cause here)
Thinking main() must be in same file as greet()
5. You have three files:
// math_ops.c
int add(int a, int b) { return a + b; }
// math_ops.h
int add(int a, int b);
// main.c
#include
#include "math_ops.h"
int main() {
printf("Sum: %d\n", add(3, 4));
return 0;
}
Which is the correct way to compile and link these files to produce an executable named calc?