Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Ubuntu?
3.
Importance of C Programming Language
4.
How to Run C Program in Ubuntu
5.
Example 1
5.1.
C++
6.
Example 2
6.1.
C++
7.
Frequently Asked Questions
7.1.
What is Linux?
7.2.
What is Network bonding in Linux?
7.3.
What is the purpose of the Ubuntu operating system?
7.4.
Why Ubuntu is better than Windows?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to Run C Program in Ubuntu

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The C programming language is well-known for its extensive function library which provides a rich set of functions and macros that programmers can use to perform various tasks.. In Linux, a C program does not run without a compiler. As a result, a separate compiler is required to compile programming languages in Linux distributions.

How to Run C Program in Ubuntu

In this blog, we will discuss Ubuntu, the C programming language, how to run a C program in Ubuntu, and some code examples.

What is Ubuntu?

Linux is the backbone of the Ubuntu operating system. Supported devices include network servers, PCs, and smartphones. The system was developed by UK-based Canonical Ltd. The Ubuntu software was developed using open-source software development principles as the foundation for all of its design decisions.

Due to its simplicity, Canonical's Ubuntu Desktop Linux distribution is among the most well-liked operating systems. It's also among the most widely used Linux distributions for beginners. It's an operating system built on the 1991 release of Linus Torvalds' Linux kernel, a UNIX-like system. Many Linux variants are great substitutes for popular operating systems like Windows and macOS since they are frequently free and open-source.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Importance of C Programming Language

C is a procedural language with structured programming support; it has a static system and a compiler written in C. C has become a computing history milestone and the most significant component throughout the computer industry since its release.

Dennis Ritchie wrote an article in which he outlined the following reasons for C's success:

  • Unix's success was the most essential element since Unix utilized C to construct its utilities.
     
  • C remained a simple and small language designed for the development of system components. C was thought to be abstract enough for humans to read, hence it is classified as a middle-level language.
     
  • C was not intended to be used in isolation but rather to develop useful tools that interact with massive systems. C is viewed as a tool for creating larger tools. Furthermore, C's core library meets the basic needs of most programmers without overburdening them.
     

As C interacts so closely with the processor, performance-critical applications are generally written in it, and because of its unparalleled popularity, programming languages developed a familiar C-style syntax in addition to the set of constructs that came with C (e.g., pointers, macros, and so on). C has evolved into the foundation of modern computing systems, including operating systems, compilers, interpreters, third-party libraries, and databases.

Also see, Floyd's Triangle in C

How to Run C Program in Ubuntu

To launch a C program on the Ubuntu operating system, follow these steps:

  • Step 1: Initially, confirm that the GCC compiler is installed on your Ubuntu operating system. You can use the following command to check the installation status of the GCC compiler:
    $ gcc -version
Check gcc Version
  • Step 2: Create a C program file using the touch command on the terminal.
    $ touch prog.c
Create a C program file
  • Step 3: Create a C program by opening the prog.c file in the text editor.
     
  • Step 4: Compile the prog.c program file in the terminal with the gcc prog.c -o prog command. In the same directory, it will create a hello executable file.
Compile C program file
  • Step 5: Using the ./prog command, launch the prog executable file on the terminal.

Example 1

In this example, we will demonstrate how to run a basic C program that checks whether the given input is an odd or even number. 

Code:

  • C++

C++

#include<stdio.h>
int main()
{
// This variable is to store the input number
int num;

printf("Enter an integer: ");
scanf("%d",&num);

// Modulus (%) returns remainder
if ( num%2 == 0 )
printf("%d is an even number", num);
else
printf("%d is an odd number", num);

return 0;
}

 

Output:

Output of Example 1

Explanation:

The above C code is a basic program that determines whether an integer entered by the user is even or odd. It begins by asking the user to enter an integer. The modulus operator (%) is then used to compute the remainder of the entered integer when divided by 2. If the remainder is 0 the software prints the even number otherwise it prints a message as an odd number. 

Example 2

In this example, we will illustrate how to run a C program to find the number of prime integers between the first and last variables.

Code:

  • C++

C++

#include <stdio.h>
int main() {
int first=10, last=20, i, flag;
printf("Prime numbers between %d and %d are: ", first, last);

// iteration until first is not equal to last
while (first < last) {
flag = 0;

//Ignore numbers less than 2
if (first <= 1) {
++first;
continue;
}

//If the first is a non-prime number, the flag will be 1
for (i = 2; i <= first / 2; ++i) {

if (first % i == 0) {
flag = 1;
break;
}
}

if (flag == 0)
printf("%d ", first);

// to check prime for the next number
// increase first by 1
++first;
}

return 0;
}

 

Output:

Output of Example 2

Explanation:

The above C code uses a loop to find and print prime values between 10 and 20. It determines if a given integer in the range may be divided by any number up to half of itself. A number is reported as prime if it has no divisors other than 1 and itself. The program moves through the range skipping all values less than two.

Also read, odd or even program in c

Frequently Asked Questions

What is Linux?

Linux is an open-source Unix-like computer OS that directly manages the resources and hardware of a system, like storage, memory, and CPU, and handles the communication between hardware and software. It's freely distributable and is basically established around Linux Kernel.

What is Network bonding in Linux?

Network bonding is also called NIC teaming. It is a kind of bonding that connects two or more network interfaces to a single interface. Usually, it improves redundancy and performance generally by increasing network bandwidth and throughput.

What is the purpose of the Ubuntu operating system?

Ubuntu is a Linux-based operating system that is widely used for desktop computing, server deployment, cloud computing, software development, and education, among other things. It gives you the ability to personalize your system and provides a versatile and open-source platform for a wide range of applications and industries.

Why Ubuntu is better than Windows?

Since it is free and open-source Ubuntu is frequently preferred over Windows. It boasts improved security, stability, ease of updating, a large software repository, and community support. All of these features combine to make it a popular choice among developers and Linux users.

Conclusion

In the current era of information technology, learning to code is highly desired. The general-purpose programming language C can be used to construct programs at an advanced level as well as to master the fundamentals of programming. In this article, we discovered how to run C Programs in Ubuntu using the GCC compiler along with some working code examples.

See more, gzip command in linux

To better understand the topic, you can refer to Ubuntu Operating SystemCompile And Run a C/C++ Code In Linux, and How to install the C++ compiler on Ubuntu.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Previous article
Difference Between C and Java
Next article
Introduction to Error Handling
Live masterclass