How to Compile and Run C Program in Centos Stream

If you have recently switched to the latest CentOS Stream from Windows and don’t know how to Install C in CentOS Stream, then buddy, you came to the right place.

C programming language requires the compiler to run a program. Without a compiler, you will not be able to run the program file.

I’ll list the popular compilers available for the C programming language, such as GCC CompilerClangMinGWTurbo C, etc.

But today, we will be more focused on GCC Compiler and trust me; it is not more than a piece of cake.

Read this:- How to reset or crack the password in CentOS/RHEL?

Install Development Tools and verify

Before installing the C/C++ compiler, we have to install Development Tools to download and install all the required packages and libraries with GCC.

# yum groupinstall 'Development Tools'

Wait for a few minutes. It takes quite a bit of time to download and configure all of the required packages.

After everything is done, verify whether GCC is installed successfully or not in your Linux System. To check, type the following command.

trendoceans@centos~]$ gcc --version

gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compile and Run C Program in Centos Stream

After installing all the necessary dependencies, Now we will write a simple basic C program to print TrendOceans on screen.

Open any command-line editor like vim, nano, or any other TextEditor and write or copy-paste the following code.

#include <stdio.h> 
int main() {
   // printf() displays the string inside quotation
   printf("\nTrendOceans\n\n");
   return 0;
}

When you are done, save your file with a .c extension. In this example, I have saved with the name firstProgram.c.

As you know, the C program file or source code needs to compile otherwise system will not run the program.

To compile a file, you need to pass the following command. Before that, make sure you are on the correct path where the firstProgram.c is saved

# cc firstProgram.c -o firstProgram

OR

# gcc firstProgram.c -o firstProgram 

A compiling file is case sensitive, so make sure what you type on terminal else you will see the compilation error on terminal “cc error: firstProgram.c: No such file or directory“.

When the file get successfully compiled you will not see the any confirmation message but the executable file is created on the current directory with the program name.

To run executable file you need to type the command:

trendoceans@centos~]$./firstProgram 

TrendOceans

That’s how you compile and run a C program in your CentOS Stream. If you stuck somwhere or having some query feel free to ask in the comment section.

Leave a Reply