How to compile and execute a c program in linux

How to compile and execute a c program in linux

Question: How to compile and run c program in linux operating system

Answer: To compile a c program use “gcc command” and to run a c program use “./” .

 

How to compile and execute a c program in linux

 

In this post i am going to explain you how to compile and run c program in linux operating system. If you are a programmer and working on Linux / Unix like operating system you should aware of how to compile and execute c program in linux / Unix operating system. Lets see how to accomplish the task with an example

In my scenario i am creating a basic “Hello World” c program with file name helloworld.c

#vim helloworld.c

#include <stdio.h>
int main(void){
printf(“Hello World\n”);
return 0;
}

 

How to compile and execute a c program in linux

 

To compile C program helloworld

#gcc helloworld.c

If their is any error in the program it will tells you while compile itself

Before executing a c program make it as an binary executable file using below command.

#gcc helloworld.c  -o helloworld

How to verify a file is a binary file or not

#file helloworld

helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

Once the c program is complied and changed in to binary file execute or run it using below command

#./helloworld

How to compile and execute a c program in linux




Leave a Reply

Your email address will not be published. Required fields are marked *