How to Run C Programs on Linux: vim + gcc

This beginner's guide explains how to set up an environment to run C programs on Linux (Ubuntu) using vim.

Introduction

When installing Linux for the first time, you might not know how to create text files or execute code.

This guide explains the steps from editing C code using vim to executing the code.

# Environment used in this post
Ubuntu 18.04.1 LTS
running on a virtual environment built with VMware on Windows 10.

Note: This article was translated from my original post.

Running C Programs on Linux

Follow these steps:

  1. Install vim
  2. Create a text file with vim
  3. Install/compile with gcc
  4. Run the file

Installing vim

First, install the vim text editor. Before installing, try checking vim on the terminal:

$ vim
Command 'vim' not found, but can be installed with:

sudo apt install vim       
sudo apt install vim-gtk3  
sudo apt install vim-tiny  
sudo apt install neovim    
sudo apt install vim-athena
sudo apt install vim-gtk   
sudo apt install vim-nox  

It kindly shows how to install vim. Let's install it right away:

$ sudo apt install vim     

That's it for the vim installation.

Creating a Text File with vim

Use vim to create a ".c" file for C language.

$ vim test.c

This opens the test.c file in the vim editor. The vim editor can be tricky at first, so be careful.

Press "i" to enter "-- INSERT --" mode and start editing text. For example, write the following code:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Hello, Lain.\n");
    return 0;
}

To put it simply, this code uses printf("Hello, Lain.\n"); to output the string "Hello, Lain.".

When you're done, press "Esc" to exit "-- INSERT --" mode. Then press ":" to enter commands.

For example:

:w to save,
:q to quit vim,
:wq to save and quit.

There are many other commands, but this is enough for now.

To delete the created file, use this command:

$ rm test.c

Installing gcc and Compiling

Source files written in C need to be compiled to create executable files. gcc is one such compiler.
According to the manual1:

~
NAME
       gcc - GNU project C and C++ compiler
~

As you can see, it's a compiler for C and C++. By the way, the gcc manual also explains various file extensions, making it quite interesting.

Just like vim, install it first:

$ sudo apt install gcc

After installation, compile the test.c file we created earlier.

$ gcc test.c -o test

This compiles test.c and creates an executable file named test.

That's it for compilation.

Running the File

To run the file, type the path of the executable file in the terminal:

$ ./test
Hello, Lain.

Your first output message has been displayed.

Although it doesn't apply to this test file, you can pass arguments when running the program:

$ ./test arg

This passes the argument arg to the program.

Uninstalling Packages

Lastly, here's how to uninstall the packages we installed.

Check the apt command manual used during installation.

~
           Removing a package removes all packaged data, but leaves usually small
           (modified) user configuration files behind, in case the remove was an
           accident. Just issuing an installation request for the accidentally removed
           package will restore its function as before in that case. On the other hand
           you can get rid of these leftovers by calling purge even on already removed
           packages. Note that this does not affect any data or configuration stored in
           your home directory.
~

remove uninstalls while leaving user config files, while purge removes everything.

Here’s how to use them:

$ sudo apt remove vim
$ sudo apt purge vim

Conclusion

This was a simple guide to setting up an environment and running C programs.

I hope this helps someone!

[Related Articles]

en.bioerrorlog.work

References


  1. How to read the manual is summarized in a previous post.