Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»C Language»How to Compile and Run C/C++ Programs in Linux

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

    By RahulJuly 13, 20212 Mins Read

    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.

    Advertisement

    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

    1
    2
    3
    4
    5
    6
    #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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #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.

    C++ g++ GCC gnu compiler
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Install Development Tools on Ubuntu, Debian & Mint

    How to Install Development Tools on CentOS, RHEL & Fedora

    C Language – An Introduction of Array

    View 3 Comments

    3 Comments

    1. Paul A. Gureghian on May 11, 2020 8:11 pm

      Why did the first program print on the same line as the prompt ?

      Reply
    2. Darothi Sarkar on July 18, 2019 9:15 am

      😉

      Reply
    3. Nana on April 3, 2018 6:25 am

      Thanks

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.