TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C Tutorials

Compile & Run a C Program on Linux

Overview

In this tutorial, we will learn the steps involved to compile and run a C Program from the command line on a Linux machine.

Steps to install GNU C/C++ compiler:

https://www.testingdocs.com/install-gcc-c-compiler-on-opensuse/

C Program

Create a hello.c file. We can use the file manager to create the file.

To create a file from the Command-line:

$ touch hello.c

or

$ vi hello.c

Open the C source code file in the text editor like vi, emacs, gedit, Kate, etc based on the Linux environment.

Write the C program source code by editing a file.

/******************************************** 
 * File:hello.c
 * Sample C Program on Linux 
 * Compile and Run from command line demo.
 * C Tutorials - www.TestingDocs.com
 ********************************************/
// header files
#include<stdio.h>
#include<stdlib.h>

// main function
int main()
{
 printf("Hello World! \n"); // print Hello message
 exit(EXIT_SUCCESS);
}

C Program Hello Linux Kate Editor

Compile

Let’s compile the C program. The gcc compiler produces an executable output file called a.out.

$ gcc hello.c

a.out is the default output executable file created when no output name is specified. We can specify the output file name with -o switch

$ gcc hello.c -o hello

Compile and Run C Program Linux

Run a C Program

Let’s run the C program with the default a.out executable output file.

$ ./a.out

Compile C Program Default Linux

To run the output file, for example, hello:

$ ./hello

This should print the Hello World! message to the terminal screen.

That’s it.

Related Posts

C Language Characteristics

C Tutorials /

C Language Characteristics

C Source Code .c File

C Tutorials /

C Program Files & Formats

GNU Compiler Collection GCC

C Tutorials /

C Compilers

C Pointers

C Tutorials /

C Pointers

C Tutorials /

C Two-Dimensional Arrays

‹ C Array Syntax› Programming Language Translators

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version