C is one of the fastest running programming language, a lot of developers start their development journey with C. You can write and compile C in Linux, Windows, and macOS.
Linux is one of the favorite operating systems among developers for its open-source and in-hand tools for development. Node JS, PHP, Python name any programming language, and you can write and run it in Linux.
Today we look into compiling and running our first C code in Linux from scratch. Before that, make sure to open your terminal using the Ctrl+Alt+T shortcut.
Table of Contents
Step 1: Install the build-essential packages
To compile and execute your C program in Linux, you need to install a build-essential package that comes with all required tools.
$ sudo apt install build-essential
Sudo users may require to enter a password before the installation begin.
Step 2: Write a basic C Program
After the installation is complete, lets us start writing our first C program from scratch.
Make sure to select your favorite Text Editor. In my case, I am going to use Gedit. Write or copy the below command and paste it into your Text Editor.
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, TREND OCEANS!");
return 0;
}
Then save the file with any name; just make sure to add the “.c” extension in the end. In my case, it’s “code.c”.
Step 3: Compiling the C program with GCC Compiler
To make your application executable, we need to compile our readable code into machine understandable code using GCC compiler.
Syntax:
$ gcc [ProgramName].c -o [OutputProgramName]
Example:
$ gcc code.c -o code
Once the above code is executed, a new file will be created in the same directory.
Step 4: Running the program
Once the executable application is created, use the below code to run.
$ ./code
Hello, TREND OCEANS!%
Below you can see the output of our basic C program. You can modify further and change the output in something different.
Let me know in the comment section if you successfully executed your first C Program. Facing any issues, drop that too.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.