how to compile c program in linux machine?

Satish Tiwary's picture
c in linux

how to run C programs on linux?

How to compile c program in linux?

There are a lot of (relatively) newer languages to choose from, but if you need something to run with high performance and less of a resource footprint, it's hard to beat C. I don't know much about bitcoin, but I do know it's supposed to be computationally expensive, making C a good language for your implementation.

It's also the primary language for Unix kernels, so if you plan to be extending (say) Linux, or fixing kernel bugs, you're going to need to know C.

Thats why after few days struggle i have started searching and testing how to compile and run diffrent programming languages on linux.

I have tested so many languages like c/c++/java/python/perl/ruby and many more.

In this article i am going to explain you about the running and compilation of a c program on Linux machine.

My frends from different countries ask me to explain this type of tutorial step wise so that it becomes easy to understand and perform.

Thats why i have used step wise method to explain running and compilation of c programs here. I have tested it on several linux flavour but here i reccomend you rhel5 or centos or fedora.

 

So lets see how to install c compiler , howto write a c program, and then how to compile them and run them on linux machine.(rhel5 here)

Tested on rhel5

 

step1:install gcc compilere first:

Gcc is a compiler c.

 

#yum install gcc*

 

 

step2:Write ur first C program

 


#vim hello.c

 #include<stdio.h>

void main()

{

printf("hello satish");

}

save it and comes out

 

 

step3:give execute permission to file.

 

#chmod 777 hello.c

 

step4:run and compile it by.


#gcc hello.c

 

this command will create an executable file a.out

 

Step5:now run a.out by


#./a.out

This will show you the output of your Program.

 

Demerits of Above method:

when you compile and run the c program in above way you will face a problem.

Problem is that , whenever you compile a new c program and compile it using gcc cprogram.c it will update the previous ./a.out file.

so a.out file will always keep changes. If you want to remove this loophole or overcome this demerit use below method to compile and run your c program.

 

For Example: I am taking the same example i have taken above.

 

Compile your c program by:

 

#gcc -o hello hello.c

 

 

Run compiled file  by:

 

#./hello

 

Now you can see your program output and you can use this technique for all your c program compilation and run.

All doubts and comments are appreciated!

Feel Free comment  for any kind of query,comment,suggestion,doubt regarding this post.

Thank you!

 

Rate this article: 
Average: 3.7 (24 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.