Learn how to use the Vim Editor for writing C programs and get an introduction to the GCC Compiler. Step-by-step guide for beginners to write, compile, and run C programs efficiently.
Vim Editor, Writing, and Introduction to GCC Compiler
Vim Editor and GCC Compiler are essential tools for writing and compiling C programs, especially in Linux and Unix environments. This guide will help beginners understand how to use Vim for coding, writing C programs, and compiling them with GCC.
-
Vim Editor in C Programming
Vim is a powerful text editor used in Unix/Linux systems. It is widely used to write C programs because of its lightweight and efficient interface.
Key Features of Vim Editor:
- Syntax highlighting for C and other programming languages.
- Efficient editing using keyboard shortcuts.
- Multiple modes: Normal mode, Insert mode, and Command mode.
- Lightweight and fast, ideal for coding on servers and low-resource systems.
Basic Vim Commands for C Programming:
- Open a file:
- vim filename.c
- Switch to Insert mode to write code: Press i.
- Save and exit:
- Save changes: Press Esc, then type :w and press Enter.
- Save and exit: Press Esc, then type :wq and press Enter.
- Exit without saving: Press Esc, then type :q! and press Enter.
-
Writing a Simple C Program in Vim
Open Vim and create a file called hello.c:
#include <stdio.h>
int main() {
printf(“Hello, World!\n”);
return 0;
}
Steps in Vim:
- Open Vim:
- vim hello.c
- Press i to enter Insert mode.
- Type or paste the above code.
- Press Esc to return to Normal mode.
- Save and exit: :wq
-
Introduction to GCC Compiler
GCC (GNU Compiler Collection) is used to compile C programs. It converts the human-readable C code into machine-executable files.
Basic GCC Commands:
- Compile a C program:
- gcc hello.c -o hello
-
- hello.c → source file.
- -o hello → output executable file name.
- Run the compiled program:
- ./hello
Expected Output:
Hello, World!
- Example: Compile and Run a C Program
C Program (addition.c):
#include <stdio.h>
int main() {
int a, b, sum;
printf(“Enter first number: “);
scanf(“%d”, &a);
printf(“Enter second number: “);
scanf(“%d”, &b);
sum = a + b;
printf(“Sum = %d\n”, sum);
return 0;
}
Steps to Compile and Run:
- Open Vim and create the file:
- vim addition.c
- Enter Insert mode by pressing i and type/paste the code.
- Press Esc and save with :wq.
- Compile the code using GCC:
- gcc addition.c -o addition
- Run the program:
- ./addition
Sample Output:
Enter first number: 10
Enter second number: 20
Sum = 30
-
Advantages of Using Vim and GCC
- Vim Editor:
- Lightweight and fast for coding.
- Supports multiple languages and syntax highlighting.
- Ideal for terminal-based development.
- GCC Compiler:
- Converts C programs into executable files.
- Supports error checking and debugging.
- Widely available in Linux, Unix, and Windows (via MinGW).
Conclusion:
Using Vim Editor along with GCC Compiler is an effective way for beginners and professionals to write, compile, and run C programs efficiently. With practice, you can master Vim shortcuts and GCC options for better programming workflow.
Ubunto C Programming
Search- terminal
Ctrl+T – open new terminal
Gcc – version
Prees enter
Sudo apt install build-essential
Clear
Compile: – gcc hello.c – o test
Run: – ./test
Gedit- open notpad
Compile: – gcc – o first first.c
Run: – ./first
Ubunto C++ Programming
g++ – – version
sudo apt install build – essential
open nano – text editor
#include<iostream>
using namespace std;
int main()
{
cout<<”Hello”<<endl;
return 0
}
Save the file – hello.cpp
Compile and run:
g++ hello.cpp –o hello
./hello
OR:
- g++ –version (check version)
- touch test.cpp (create file)
- g++ test.cpp (compile file)
- ./o.out (run program)
- g++ test.cpp – o
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System