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#

Permission denied when executing C Program on Linux(from flash drive)

I had this issue when trying my first hello world like C program on Kali linux. I am able to save and compile C program using GCC (GNU compiler collection) but could not set the permissions to it.

The reason??? the file is on a USB drive. I am not sure if mounting it via VM ware is a reason. However, could not execute it from the pen drive.

Will be very happy if someone can explain the reason 🙂

I tried it, and you can notice that I am current on c-programming folder in my pend drive.

root@kali:/media/root/PWK/lab/c-programming# ls
firstprog firstprog.c
root@kali:/media/root/PWK/lab/c-programming#

The permissions before chmod:

root@kali:/media/root/PWK/lab/c-programming# ls -l firstprog
-rw-r--r-- 1 root root 7252 Jan 13 01:05 firstprog
root@kali:/media/root/PWK/lab/c-programming#

And after chmod:

root@kali:/media/root/PWK/lab/c-programming# chmod +x firstprog
root@kali:/media/root/PWK/lab/c-programming# ls -l firstprog
-rw-r--r-- 1 root root 7252 Jan 13 01:05 firstprog
root@kali:/media/root/PWK/lab/c-programming#

Now… moved the folder to Desktop and trying the same:

root@kali:~/Desktop/c-programming# ls
firstprog firstprog.c
root@kali:~/Desktop/c-programming# ls -l firstprog
-rw-r--r-- 1 root root 7252 Jan 13 01:05 firstprog
root@kali:~/Desktop/c-programming# chmod +x firstprog
root@kali:~/Desktop/c-programming# ls -l firstprog
-rwxr-xr-x 1 root root 7252 Jan 13 01:05 firstprog
root@kali:~/Desktop/c-programming#

And now executing the same:

root@kali:~/Desktop/c-programming# ./firstprog
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#

Bingoooo!!! 🙂