Skip to content
September 26, 2007 / ranacse05

Compile C program using PHP-5

To day i successfully compile a .c program using PHP-5. It just simple.I just use “System()” function on PHP-5. 🙂

Here is an example

$data = '#include<stdio.h> 

int main(){
printf("Hello World");
return 0;


$my_file = 'code.cpp';
file_put_contents($my_file, $data);

system("gcc {$my_file} &> error.txt");

$error = file_get_contents("error.txt");

    echo $error;


Leave a Comment
  1. Sona / Mar 22 2008 8:58 am

    how did u compiled…. plz tell me how??
    i used system(“cc sam.c”); but itz not working …

  2. ranacse05 / Mar 26 2008 5:27 am

    i compile it using system(“gcc main.c”); remember u have to make the directory permission ‘0777’ .

    • Arumugam / Mar 2 2015 9:51 pm

      i am using windows operating system,please tell me complete guide to develop online c/c++ compiler using php

  3. php programmer / May 15 2008 12:33 pm

    shell_exec() function can also be called with command line arguments.
    should give the same result as system()?

  4. ranacse05 / May 16 2008 9:09 am

    @php programmer
    ya u r right 🙂

  5. ashish / May 26 2008 11:39 am

    i want to run a c program via php. the program also has scanf commands. how do i give these inputs. i canntouse command line arguments as the inputs are to be given from dynamically collected data over the time.

    • ranacse05 / Oct 25 2009 8:10 pm

      how about u read the inputs form a file ?

      i think that can help u .

  6. Rahid Hasan Robin / Oct 22 2009 8:30 am

    I want to see all compiling error in php. How could it possible pls help me.

    Thanks for this helpful post

    • ranacse05 / Oct 25 2009 8:08 pm

      u need to call an shell script for that in that shell script put the compile command and save the output of those command on another file then read the file and do what ever u want .

      there maybe some easy way too.

  7. Miljan / Apr 24 2010 3:11 pm

    i am making online compiler for college… i need to now when i assign text filed values to variable what i should put in system(gcc and then?) ? pls help its urgent

    • ranacse05 / May 17 2010 7:50 pm

      u have to filter the text before u pass that to the system, bcoz u need make sure that there is not harmful command , or u can lost ur server data 😀

  8. sharmilli / Feb 23 2011 10:04 am

    I tried shell_exec(‘gcc -0 Compile Compile’); , system(‘gcc -0 Compile Compile’);, exec(‘gcc -0 Compile Compile’); to run my C code but its not working.

    • ranacse05 / Feb 23 2011 5:48 pm

      You need to allow those functions on php.ini files , usually those are not allowed in shared hosting .

      • sharmilli / Feb 24 2011 4:22 am

        I could use the command for running ‘./compile’ but.

  9. Tinesh / Aug 8 2011 7:31 pm

    i’m trying to design an online compiler.. , i’m not able to run this command system(‘gcc hello.c) from my php file… ,but system(‘./a.out’) works

  10. rock / Nov 24 2011 12:48 am

    wud u plz send me the code i m in trble…

    • ranacse05 / Nov 24 2011 12:57 am

      Hey Rock please check the phpinfo() for the exec, system etc functions most of the hosting dont allow to call those functions , you can try in your VPS or local server .

  11. Subbaiyan / May 8 2012 2:13 pm

    c & C++ Program code input textarea in PHP and out put in div any one do this work replay to me and send the php code

  12. Dirk / Aug 21 2012 12:56 am

    I absolutely love your website.. Pleasant colors & theme. Did you make this site yourself?

    Please reply back as I’m planning to create my own site and would like to know where you got this from or exactly what the theme is called. Kudos!

  13. Rohit Gupta / Sep 12 2012 6:02 pm

    can anyone mail me those php codes

    thanks in advance…

  14. Praveer Grover / Oct 4 2012 10:57 pm

    Is it for LINUX systems or can be used for windows as well?

  15. mridula / Nov 18 2012 11:18 pm

    I need to compile code of C using php, but how to make it intrective as it need all scanf value at one time, not while needed…
    I need to resolve it as soon as possible, please help

  16. nirmala / Nov 30 2012 11:50 am

    I have written c c++ code in the text area in php5 and now i have to compile these code on localhost?
    What i should do ,anyone help me?

  17. var / Feb 22 2013 2:01 pm

    can someone plzz mail me the code for compiling c/c++ program via php on windows 7 platform.
    i m using xampp on my local machine.
    i hv no idea abt hw to do dat…plzzzz help me..
    .its very urgent…

  18. monisha / Aug 9 2013 2:35 pm

    hi, i have to do my php project i.e online c compiler.. can you help me how to convert text file into c file???

    • akshayKosandar / Feb 19 2017 12:53 am

      Have u did it??
      Bcz i want to know how u did ?
      I have also d same project plz help!!

  19. neeraj / Aug 19 2013 3:05 am

    sir how can i run c program though php on windows plzz give sample example

  20. E.Rameshkumar / Aug 27 2013 11:19 am

    hai sir, i want to build an online c/c++ compiler through using php. as i complete my design work. in my design page i have one richtext box and one command button and one label box. i go to put my c coding from richtext box and click the button. after click the button my code was compile and exc as background process. then my result will be display to my label box. this is my situation. how will create a coding for this. i request you to plz sent appropriate code for the following mail. (, plz help me sir.

  21. Vidhya / Sep 6 2013 12:56 pm

    How to compile c code in php and get the output???Could u plz post the code ..

  22. Rosiya / Sep 6 2013 12:57 pm

    How to compile c code in php………????? plz anyone post the code…plzzzzzzzzzzzzzz…

  23. Vidhya / Sep 6 2013 1:00 pm

    How to compile and execute a c code in php program??Plz post the code…

  24. Sri Harsha Chilakapati / Jul 22 2014 10:44 pm

    Nice post, but is it really safe? Users can type malicious code right?

    • ranacse05 / Jul 22 2014 10:46 pm

      Before you run the code just check for system() and comment that line and you’ll be fine .

    • Praveer / Jul 23 2014 10:35 am

      No its not anybody can drop a fork bomb anytime. To make it safe you have to 1st create a sandbox and run the programs in that sandboxed environment.

  25. Pankaj / Aug 6 2014 5:03 pm

    i run this code on mu local xampp server but give blank …….how do i this pls comment

    • ranacse05 / Aug 6 2014 6:43 pm

      Check if system() is turn off or not in your system .

      • Pankaj / Aug 7 2014 10:55 am

        thanks for fast reply .how to check system() is turn off or not in your system.i am compile my c code on gcc through command prompt. and if you have tutorial regarding this pls mail my id Thanks

  26. causing havoc / Sep 3 2014 2:56 am

    It’s an awesome paragraph designed for all the internet viewers; they will obtain benefit
    from it I am sure.

  27. Naush / Nov 3 2014 6:25 pm

    I Am New In PHP Can You Give More Descriptive Tutorial About This ?

  28. pramodgupta / Dec 13 2014 12:26 am

    hey i’m getting this error plz help : Warning: file_get_contents(error.txt): failed to open stream: No such file or directory in C:\wamp\www\test.php on line 16
    Call Stack.

    and what is use of this symbol system “&>” in line number 14.
    system(“gcc {$my_file} &> error.txt”);

  29. Arumugam / Mar 2 2015 9:59 pm

    please tell me complete tutorial to develop online compiler using php in windows

  30. Jekil / Mar 30 2015 11:32 pm

    Hey i want to make C++ compiler online means i want to embed terminal into website because i want to make Graphics program to run online is it possible ? if yes then how to do it ? please tell me my email id is Thank You

  31. mahendra / Apr 3 2015 11:38 am

    sir when i run this code ,didnt work, it generate error

    • ranacse05 / Apr 3 2015 4:02 pm

      make sure you have the permission to run the function .

  32. vicky / Apr 6 2015 6:56 pm

    sir would you please tell how to check these permissions . i am desining a online compiler n finding it very hard to design

  33. vicky / Apr 6 2015 7:04 pm

    and are these are woking in windows based os or linux

  34. praveenkanwar0 / Jan 11 2016 4:26 pm

    Hello there. I have been doing cloud compiler project for my college lab. I am able to compile different languages. But how can i make it interactive to allow user inputs. Could you please give me basic idea on this. Thanks in advance!!

  35. Lgm / Feb 23 2016 2:24 am

    sir please tell me how to run c/c++/java any language in my own website..i have developed a website for coding competitions but i am using hackerrank compeler…plz help me sir how to run in by using php or do we have to set any plugins.

  36. Prashant / May 9 2016 5:30 pm

    Hi, can I run a php script by C code??

  37. venkat / Sep 23 2016 12:34 pm

    How I can execute above code on turbo c compiler with windows7 specification.

  38. venkat / Sep 23 2016 12:35 pm

    How i can execute above code on turbo c windows 7 operating system.

    • ranacse05 / Mar 2 2017 10:31 pm

      Not possible in turbo C you need a command line enable compiler like GCC

  39. Company Buying / Oct 26 2016 3:41 pm

    Hmm it appears like your blog ate my first comment
    (it was extremely long) so I guess I’ll just sum
    it up what I had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any suggestions for newbie blog writers? I’d
    definitely appreciate it.

  40. Mohanraj / Mar 2 2017 8:20 pm

    how to execute theis program plzz explain in details. I cant able to run

    • ranacse05 / Mar 2 2017 10:30 pm

      please check exec or system functions on php .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: