Tutorial Compiling A C Program[ LINUX ]

Code_Block

Active Coder
May 24, 2019
187
53
38
github.com
So, you're starting off in the C Programming-Language. Once a popular Language that is still used but just less used than C++. Well, if you are learning C and you want to know how to Compile your Programs then you've come to the right place.

NOTE: This is a Tutorial for Linux-Systems. It has been tested on Linux Ubuntu 19.04(Linux Kernel 5.0). I cannot confirm if it will work on a MacOS X-System or a Windows-System with MinGW installed(MinGW is required to use C on Windows).

Anyways, let's get started. First, open up your Text-Editor. I'll be using the default Text-Editor that comes with Ubuntu but you can use anything else.

Once you've opened up your Text-Editor, write the following Code:
hello.c:
#include <stdio.h>

int main(void) {
    printf("Hello world!\n");

    return 0;
}
Once you've written that, save your File as hello.c

Now we'll get onto the Compiling part of our Program. When we Compile, we'll then have a Program which we can then run by executing Commands.

First, open up your Terminal. You can search through your Computer for it but to open it faster, just press ctrl+alt+t

Now that you've opened up Terminal, make sure you have GCC(GNU-Compiler-Collection). You'll need GCC to Compile your C-Code. If you don't have it installed then there are plenty of Tutorials online dedicated to installing it on your System.

If you do happen to have GCC or have now installed it, execute the following Command:
gcc hello.c -o hello
This will Compile your C Program which can then be ran in the Terminal. To run your Program, enter the following into Terminal:
./hello
Once you've entered that into Terminal, your Program will run and then "Hello world!" will be displayed in Terminal.

Well, congratulations. You've just compiled your first C Program. See, it wasn't that hard.

Good job!
 
  • Like
Reactions: Malcolm