Programming in C using gcc in Linux
Installing GCC and Writing Your First C Program
1️⃣ Check Whether GCC Is Installed
If you are using Linux or UNIX, check if GCC is installed:
If GCC is installed, version details will be displayed.
If not installed, install it using instructions from:
https://gcc.gnu.org/install/
2️⃣ Install GCC on Ubuntu
Method 1: Install GCC Directly
Open terminal and run:
(Example: Ubuntu 18.04 Bionic Beaver / 20.04 Focal Fossa)
Method 2: Install Using build-essential (Recommended)
This installs:
gcc compiler
g++ compiler
required libraries
3️⃣ Check GCC Version After Installation
4️⃣ Writing Your First C Program
Example Program
Output
5️⃣ Program Explanation (Line by Line)
Line 0
Multi-line comments in C are written between
/* */Comments are ignored by the compiler
Improve readability and documentation
Single-line comments use
//
Line 1
Lines starting with
#are handled by the preprocessorPreprocessor processes header files before compilation
stdio.hcontains declarations for functions likeprintf()Header files usually end with
.h
Line 2
Entry point of every C program
Execution begins from
main()void→ no parameters passedint→ return type of the function
Line 3 & 6
Curly brackets define scope
Used in functions, loops, conditions
Every function must start and end with
{ }
Line 4
Standard library function for output
Semicolon
;marks end of statementMandatory in C
6️⃣ Compile and Execute a C Program
Step 1: Save the file
Save as:
Step 2: Compile
If successful → creates executable:
Step 3: Run Program
Output:
Specify Output File Name Using -o
Output:
✔ Ensure:
gcc is in PATH
you are in the same directory as
hello.c
7️⃣ Important GCC Compiler Options
1. Specify Output Executable Name (-o)
Creates executable named hello.
2. Enable All Warnings (-Wall)
Shows all compiler warnings.
3. Produce Only Preprocessor Output (-E)
Generates preprocessed file.
4. Produce Assembly Code (-S)
Creates assembly output.
5. Produce Object File Only (-c)
Creates:
(Object/machine-level compiled code without linking)
6. Save All Intermediate Files (-save-temps)
Output may include:
Stores all compilation stages.
7. Link With Shared Libraries (-l)
Links program with shared library libCPPfile.
8. Show Verbose Compilation Steps (-v)
Displays detailed compilation steps.
9. Provide GCC Options From a File (@ option)
Create options file:
Compile using options file:
8️⃣ Online C Compiler
You can also run C programs online:

Comments
Post a Comment