Executing C Programs on a Linux Environment

Learn how to install Linux on your machine, and practice compiling and running C programs on a Linux environment.

Install Linux

You can install Linux on your local machine and, then, create and execute programs. You can download the Ubuntu Linux from here.

Step 1: Write source code

You can type the program into any suitable editor. Type the program in a text editor and save it as hello.c. After that, you will need to compile and execute the program.

Step 2: Compile the program using the gcc compiler

Compile the program using the gcc compiler or by running either one of the following commands:

  • gcc hello.c
  • gcc hello.c -o hello.out

By default the executable code for any program is stored in the file: a.out. To store it in any other file use the switch, –o filename.

Step 3: Execute the program

After the successful compilation of the program, you have to execute it. ./a.out means execute a.out from the current directory. Execute the program by running one of the following commands:

  • ./a.out
  • ./hello.out

Get hands-on with 1200+ tech skills courses.