0
0
Cprogramming~30 mins

Splitting code into multiple files - Mini Project: Build & Apply

Choose your learning style9 modes available
Splitting code into multiple files
📖 Scenario: You are creating a simple C program that prints a greeting message. To keep your code organized, you want to split it into two files: one for the main program and one for the greeting function.
🎯 Goal: Build a C program with two files: main.c and greet.c. The greet.c file will have a function that prints a greeting. The main.c file will call this function.
📋 What You'll Learn
Create a function called print_greeting in greet.c that prints "Hello from greet.c!"
Declare the function print_greeting in a header file greet.h
Include greet.h in main.c
Call print_greeting from main.c
Compile both files together to run the program
💡 Why This Matters
🌍 Real World
Splitting code into multiple files helps keep programs organized and easier to maintain, especially as they grow larger.
💼 Career
Many software projects require working with multiple files and modules. Understanding how to split and compile code is essential for professional C programming.
Progress0 / 4 steps
1
Create the greeting function in greet.c
Create a file named greet.c and write a function called print_greeting that prints the text "Hello from greet.c!\n" using printf. Include <stdio.h> at the top.
C
Need a hint?

Remember to include <stdio.h> to use printf.

2
Create the header file greet.h
Create a header file named greet.h. Declare the function print_greeting with the signature void print_greeting();. Use include guards to prevent multiple inclusion.
C
Need a hint?

Use #ifndef, #define, and #endif to create include guards.

3
Write main.c to call print_greeting
Create a file named main.c. Include greet.h. Write the main function that calls print_greeting() and returns 0.
C
Need a hint?

Include the header file with quotes: #include "greet.h".

4
Compile and run the program
Compile main.c and greet.c together using gcc main.c greet.c -o greet_program. Then run the program with ./greet_program. The output should be Hello from greet.c!
C
Need a hint?

Use the command gcc main.c greet.c -o greet_program to compile and ./greet_program to run.