How To Compile And Run a C/C++ Code In Linux
Last Updated :
26 Dec, 2024
C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance.
In this article, we will be compiling and executing the C Programming Language codes and also C++ codes with various compilers like CC, GCC, and G++ compilers.
Compile And Run C Code in Linux
Compiling and running C code in Linux is possible with built-in tools like CC and GCC compilers. Below are two efficient methods to compile and execute your programs:
Method 1: Using CC Compiler
In this method, we will be compiling and executing the C program code using CC Compiler.
Step 1: First, we need to open the text editor and terminal for writing code and executing it through the terminal.

Step 2: In the text editor we need to write any code using a C programming language.
Example Script:
C
#include <stdio.h>
int main() {
printf("welcome to geeks for geeks");
return 0;
}
We have written a simple C program to print the “welcome to geeks for geeks” message.
Step 3: Now, save the file with the .c extension. So in this example, we have saved the file as welcome.c.

Step 4: Now compile and run the C code in the terminal using the commands below.
ls
cc welcome.c
./a.out

cc Compiler
Our written code has been executed and the output is displayed in the above screenshot.
Method 2: Using GCC Compiler
We can also compile and execute the script using the GCC compiler. So, let’s understand the compilation and execution using GCC example below.
Example Script:
C
#include <stdio.h>
int main() {
// code
int a=10,b=20,c;
c=a+b;
printf("addition of=%d\n",c);
return 0;
}
Step 1: Navigate to the directory where the file is been saved. Use the below commands.
In this example we are already in the current directory.
Step 2: Execute the command below for compilation and execution.
cc -o add add.c
./add

GCC compiler
In the above image, we have written a simple C program for the addition of two numbers.
Compile And Run C++ Code in Linux
In this method, we will be compiling and executing the C++ program code using G++ Compiler.
Step 1: Write the C++ program code in a text file using a text editor and save the file with the .cpp extension.
Example Script:
C++
#include <iostream>
using namespace std;
int main() {
cout << "Welcome To Geeks For Geeks For Geeks";
return 0;
}
Step 2: Navigate to the directory where the file is saved.
In this example we are already in the current directory.
Step 3: Execute the command below for compilation and execution.
g++ cplus.cpp
./a.out

g++ compiler
In the above image, we have written a simple C++ program to print the “Welcome to GeeksforGeeks” message.
Conclusion
In this article we discussed how we can compile and run C and C++ programs in Linux using various compilers. C is designed for system programming and kernel development, while C++ excels in performance for games and applications. The methods demonstrated with CC, GCC, and G++ compilers equipped readers to compile and execute code efficiently. We discussed step-by-step instructions and example scripts. Overall, we can say that this article will help programmers to successfully run their C and C++ programs on Linux systems.
How To Compile And Run a C/C++ Code In Linux – FAQs
How to compile a C or C++ code on Linux?
Step 1: Install the compiler: sudo apt install gcc g++.
Step 2 Save your code in a file with a .c (for C) or .cpp (for C++) extension.
Step 3: Open the terminal and navigate to the file’s directory using cd.
Step 4: For C code, compile with gcc program.c -o program, or for C++ code, use g++ program.cpp -o program.
Step 5: Run the executable with ./program
Does Linux use C or C++?
Linux primarily uses C as its core programming language. The Linux kernel, which is the foundation of the operating system, is written in C with a small amount of assembly code. While C++ is not used in the Linux kernel, it is widely supported in Linux environments for application development.
How do I check if GCC is installed on my Linux system?
Run the command gcc --version in the terminal. If GCC is installed, it will display the version number.
Can I compile and run code without using the terminal?
Yes, you can use an IDE like Code::Blocks, CLion, or VS Code, which provides a graphical interface for compiling and running code.
Similar Reads
How To Compile And Run a C/C++ Code In Linux
C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to Compile, Decompile and Run C# Code in Linux?
C# is a modern multi-paradigm programming language developed by Microsoft and released in the year 2000. By multi-paradigm we mean that it includes static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented, and component-oriented programming discip
2 min read
How to Compile and Run C/C++/Java Programs in Linux
C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and clean s
3 min read
VS Code | Compile and Run in C++
In this article, we will learn how to compile and run C++ program in VS Code. There are two ways of doing that you can use any one of them as per your convenience. It is to be noted that a majority of competitive programmers use C++, therefore the compilation and execution of the program needs to be
3 min read
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read
How to Install Code Blocks for C++ on Linux?
Code::Blocks is a free IDE( an integrated development environment), for C/C++ and FORTRAN languages. It is a cross-platform IDE and available for Windows, Mac, and Linux, In this article, we are going to discuss various methods using which we can install Code Blocks on Linux.: Installation Code Bloc
2 min read
How to Compile and Run C program in Terminal?
To compile and run the C programs efficiently users often use a compiler or system in-build terminal. In that case, the well-known system terminal called CMD or command prompt can also be used to process the C programs with required outputs. To create an executable C program on the Windows system co
7 min read
How to compile 32-bit program on 64-bit gcc in C and C++
Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version. Well it would be a good option in terms of speed purposes. But it would lead to problem, if someone wants to run their program as a 32-bit rather than 64-bit for testing or debugging purposes. Therefore we must ha
2 min read
how to profile C++ code running in Linux
In Linux, Profiling C++ code is an important process in optimizing and understanding the overall performance metrics of our program. The profiling process consists of the tasks of analyzing the execution time of various functions which we have implemented in our code, this also allows us to identify
6 min read
How to Install GCC Compiler on Linux?
In this article, we will discuss how to install a GCC compiler on Linux. GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++. The GCC is an open-source collection of compilers and libraries. Let's st
2 min read