Compiling C Program in Linux

C code must be compile into machine instructions before it can be executed.

GCC compiler (GNU compiler collection) is free compiler that translates C into machine language that a processor can understand.  Compiling a C code generates binary code.  The binary’s instructions are written in machine language, an elementary language the CPU can understand.

Each architecture has different machine language. Compilers are designed to translate the language of C code into machine language for a variety of architectures.

Example:

Below is a hello world program named helloworld.c

#include <stdio.h>

int main()
{
 int i;
 for (i=0; i< 10; i++) {
 printf("Hello World!\n");
 }
 return 0;
}

To compile the c program:

root@kali:~/Desktop/c-programming/demo# gcc -o helloworld helloworld.c
root@kali:~/Desktop/c-programming/demo#

Executing the above command to compile the C program generates a binary executable file helloworld:

This compiled file requires executable permission to be executed. We can ls command to verify existing permissions on file:

root@kali:~/Desktop/c-programming/demo# ls -l helloworld
-rwxr-xr-x 1 root root 7252 Jan 21 00:31 helloworld

As the file is already executable, we can run the binary:

root@kali:~/Desktop/c-programming/demo# ./helloworld
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
root@kali:~/Desktop/c-programming/demo#