Writing, Compiling, and Running a C Program
1. What is a C Program?A C program is a set of instructions written in the C programming language that tells the computer what to do. C is one of the oldest and most powerful programming languages. It's used in everything from mobile devices to operating systems.
2. Steps to Create and Run a C Program
To run a C program, follow these three main steps:
Step | Description | Tool Needed |
---|---|---|
1️⃣ | Write the Code | Text Editor (VS Code, Notepad, etc.) |
2️⃣ | Compile the Code | C Compiler (like GCC) |
3️⃣ | Run the Program | Command Line / Terminal |
✍️ Step 1: Writing a C Program
This is where you type your code into a text editor and save it with a .c extension.
✅ Basic Syntax of a C Program:
#include <stdio.h> // Include input/output library
int main() { // Start of main function
printf("Hello, World!\n"); // Print message
return 0; // End of program
}
#include <stdio.h>
– Tells the program to use the standard input-output library.int main()
– The starting point of any C program.printf()
– Prints text on the screen.return 0;
– Ends the program successfully.
⚙️ Step 2: Compiling the Program
The C code you wrote cannot be directly run by the computer. First, it must be converted (compiled) into a language the machine understands.
✅ Compilation Command (GCC):gcc hello.c -o hello
📌 This command says:- Compile
hello.c
- Create an output file named
hello
(orhello.exe
on Windows)
If there are no errors, this will create an executable file.
▶️ Step 3: Running the Program
Now that it's compiled, you can run the program to see the result.
✅ Run Command:On Windows:
hello
On Linux/Mac:./hello
🖨️ Output:Hello, World!
🎉 Congratulations! You just ran your first C program.
📘 Example Program: Adding Two Numbers
#include <stdio.h>
int main() {
int a = 5, b = 7;
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Sum = 12
🧠 Quick Summary
Task | What You Do |
---|---|
Write Code | Use a .c file and write in C language |
Compile Code | Use gcc filename.c -o outputname |
Run Program | Use ./outputname or outputname.exe |
See Output | Enjoy the result on the screen |