SlideShare a Scribd company logo
System
Programing
Execution of C Program in Linux
How to Write and Run a C-Program in Linux
Linux is a developer’s paradise because it is an open-source operating
system, freely available and offers free programming tools for all popular
programming languages. In this article, we will explain you how to write,
compile and run a simple C program on Ubuntu Linux. This will serve you as
a foundation to move on to more complicated and useful C programs that
you can write and run on Linux.
We have run the steps and commands mentioned in this article on an Ubuntu
22.04 LTS system, but it works exactly the same on other versions like
Ubuntu 20.04 or on Debian 11.
To compile a simple C program, we use the Linux command-line tool, the
terminal. To open the terminal, you can use the Ubuntu Dash or the key
combination Ctrl+Alt+T.
Step 1: Install the build-essential packages
In order to compile and execute a C program, you need to have the essential
packages installed on your system. Enter the following command as root in your
Linux Terminal:
$ sudo apt-get install build-essential
You will be asked to enter the password for root; the installation process will
begin after that. Please make sure that you are connected to the internet
Step 2: Write a simple C program
After installing the essential packages, let us write a simple C program.Open
Ubuntu’s graphical Text Editor and write or copy the following sample program
into it:
#include<stdio.h>
int main()
{
printf("nA sample C programnn");
return 0;
}
Then save the file with .c extension. In this example, I am naming my C program
as sampleProgram.c
Step 3: Compile the C program with gcc Compiler
In your Terminal, enter the following command in order to make an executable
version of the program you have written:
Syntax:
$ gcc [programName].c -o programName
Example:
$ gcc sampleProgram.c -o sampleProgram
Make sure your program is located in your Home folder. Otherwise, you will need
to specify appropriate paths in this command.
Step 4: Run the program
The final step is to run the compiled C program. Use the following syntax to do
so:
$ ./programName
You can see how the program is executed in the above example, displaying the
text we wrote to print through it.

More Related Content

DOCX
C language industrial training report
DOC
How to compile and run a c program on ubuntu linux
PDF
OS Lab 02.pdfdfzdzxdxdxdxgfxfxxgxgfxgfgfx
PDF
How to Install GCC Compiler on Debian 12.pdf
PDF
Linux shell ggsipu-lug
PDF
C programming first_session
PDF
C programming first_session
PPTX
Process (OS),GNU,Introduction to Linux oS
C language industrial training report
How to compile and run a c program on ubuntu linux
OS Lab 02.pdfdfzdzxdxdxdxgfxfxxgxgfxgfgfx
How to Install GCC Compiler on Debian 12.pdf
Linux shell ggsipu-lug
C programming first_session
C programming first_session
Process (OS),GNU,Introduction to Linux oS

Similar to How to run C Program in Linux (20)

PDF
IP Lab Manual for Kerala University 3 Year UG Programme
DOCX
The basics of c programming
PPTX
Introduction to c language
PDF
GNU Compiler Collection - August 2005
PDF
QE-SSP - Lecture 2: Productivity tools: Linux & JupyterLab
DOCX
Adding System Call to Kernel
PDF
Fundamental of Information Technology - UNIT 7
PDF
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
PPTX
Linux Simple Introduction
PDF
1 c introduction
PPT
Linux: Beyond ls and cd
PDF
Linux introduction
PPT
PDF
PDF
Embedded Operating System - Linux
PPT
Advanced c programming in Linux
PPT
lect01.ppt
IP Lab Manual for Kerala University 3 Year UG Programme
The basics of c programming
Introduction to c language
GNU Compiler Collection - August 2005
QE-SSP - Lecture 2: Productivity tools: Linux & JupyterLab
Adding System Call to Kernel
Fundamental of Information Technology - UNIT 7
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Linux Simple Introduction
1 c introduction
Linux: Beyond ls and cd
Linux introduction
Embedded Operating System - Linux
Advanced c programming in Linux
lect01.ppt
Ad

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
AI in Product Development-omnex systems
PDF
Digital Strategies for Manufacturing Companies
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
top salesforce developer skills in 2025.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
2025 Textile ERP Trends: SAP, Odoo & Oracle
AI in Product Development-omnex systems
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms II-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Essential Infomation Tech presentation.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
wealthsignaloriginal-com-DS-text-... (1).pdf
How Creative Agencies Leverage Project Management Software.pdf
history of c programming in notes for students .pptx
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
top salesforce developer skills in 2025.pdf
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Ad

How to run C Program in Linux

  • 2. How to Write and Run a C-Program in Linux
  • 3. Linux is a developer’s paradise because it is an open-source operating system, freely available and offers free programming tools for all popular programming languages. In this article, we will explain you how to write, compile and run a simple C program on Ubuntu Linux. This will serve you as a foundation to move on to more complicated and useful C programs that you can write and run on Linux. We have run the steps and commands mentioned in this article on an Ubuntu 22.04 LTS system, but it works exactly the same on other versions like Ubuntu 20.04 or on Debian 11. To compile a simple C program, we use the Linux command-line tool, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T.
  • 4. Step 1: Install the build-essential packages In order to compile and execute a C program, you need to have the essential packages installed on your system. Enter the following command as root in your Linux Terminal: $ sudo apt-get install build-essential You will be asked to enter the password for root; the installation process will begin after that. Please make sure that you are connected to the internet
  • 5. Step 2: Write a simple C program After installing the essential packages, let us write a simple C program.Open Ubuntu’s graphical Text Editor and write or copy the following sample program into it: #include<stdio.h> int main() { printf("nA sample C programnn"); return 0; } Then save the file with .c extension. In this example, I am naming my C program as sampleProgram.c
  • 6. Step 3: Compile the C program with gcc Compiler In your Terminal, enter the following command in order to make an executable version of the program you have written: Syntax: $ gcc [programName].c -o programName Example: $ gcc sampleProgram.c -o sampleProgram Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths in this command.
  • 7. Step 4: Run the program The final step is to run the compiled C program. Use the following syntax to do so: $ ./programName You can see how the program is executed in the above example, displaying the text we wrote to print through it.