How to Compile and Run C/C++ Programs in Linux

Channel: Linux
Abstract: which you can run on your system. This tutorial will help you to run a C/C++ program in Linux/Unix system through the command line. We will use ‘gcc’

C is a robust, structured programming language used for developing system software. By the design, C provides constructs that could be map efficiently to typical machine instructions. It was developed by Dennis Ritchie in Bell labs. The c program source is free-format text, using the semicolon as a statement terminator and curly braces for grouping the blocks of statements like conditions, functions or loops.

C is a compiled programing language. After creating a C program, first, you need to compile it using C compilers. It will generate the binary file, which you can run on your system.

This tutorial will help you to run a C/C++ program in Linux/Unix system through the command line. We will use ‘gcc’ and ‘g++’ commands from GCC (GNU Compiler Collection) to compile a C/C++ program. Here:

  • gcc is the GNU C Compiler from GCC.
  • g++ is the GNU C++ Compiler from the GCC.
Intalling Development Tools

In order to run a C Program, You must have installed Development Tools packages on your Linux system. Run one of the following commands to install development tools packages as per your operating system.

  • Redhat based systems:
    sudo yum groupinstall "Development Tools" 
    
  • Debian based systems:
    sudo apt-get install build-essential   
    
Create Hello World Program in C

For the example, I have selected C hello world program. Create a new file on your system as follows:

vim helloworld.c 

using the below content

#include <stdio.h> int main() { printf("Hello World!"); return 0; }123456#include <stdio.h>int main(){   printf("Hello World!");   return 0;}

Compile And Run C/C++ Programs In Linux

I used GNU C Compiler to compile the above hello world C program as following:

gcc helloworld.c -o hello 

[OR] Use the below command to use C++ compiler.

g++ helloworld.c -o hello 

The above commands will create an executable file named hello in your current directory. You can directly run that same as other commands.

./hello 

You can also copy the file under the bin directory (/usr/local/bin) to make them accessible system-wide.

Try Another C Example

Let’s try with another C program with user input. For this example, I used this sample C program to input two integers from the user and calculate the sum of them.

#include<stdio.h> int main() { int i, j, sum; scanf("Enter 1'st integer: %d", &i); scanf("Enter 2'nd integer: %d", &j); sum = i + j; printf("Sum is: %d\n", sum); return 0; }1234567891011121314#include<stdio.h>int main(){      int i, j, sum;        scanf("Enter 1'st integer: %d", &i);    scanf("Enter 2'nd integer: %d", &j);        sum = i + j;     printf("Sum is: %d\n", sum);    return 0;}

Now compile and run a C program

gcc sum.c -o sum 
./sum 

Conclusion

In this tutorial, you have learned to compile and run a C program in Linux system via command line.

Ref From: tecadmin

Related articles