Sunday, February 09, 2014

Creating Website using C programming Language - read|more at

C program is a powerful programming language.number of web interpreter languages and database are built by this C language.even unix,Most of Windows and other basic network are driven by the C language.we can't imagine without c language and thanks to Dennis.So,we are going to built a simple website using c program and compile using GCC compiler and execute as CGI using Apache server.Thanks to Shuvojit Sarkar - programmer for configuring my linux environment successfully.


Apache server 2+ version is most preferable(Apache 2.2.22)
GCC compiler 
Text Editor :D

configure Apache server:

Assuming you have already Apache server running or follow below instructions
 sudo apt-get install apache2 

after installation try to open your browser and visit your localhost and it should show the message Working!

You will need to use the CGI interface of Apache 2 webserver. So first find out the path for the cgi-bin directory. You can see it in the /etc/apache2/sites-available file where it mentions:
   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all

   ErrorLog ${APACHE_LOG_DIR}/error.log
Finally it should look like above.If you dont have just try to copy it and configure it.

check now http://localhost/cgi-bin/ works without any errors,i mean with error message of Forbidden

Now let us code C language to run in Web browser.
there are two ways to running C program to run in web browser via apache server.
1)the first way is to directly code the html codes in printf(); function which would be tough in huge file of html.
2)the second way is to create txt file and using file operation to import the text file and read it using the loop concept and generate html codings.
hope you got the clear idea on this.

Code easily:

 create a C file using text editor.

#include <stdio.h>
int main()
    printf ("Content-Type: text/html\n\n");
    printf ("<html>");
    printf ("<head><title>C-Html sync</title></head>");
    printf ("<body>");
        printf ("<img src=\" \"");
     printf ("This page is created by a C program.<br />");
    printf ("I love to share my ideas to the world <strong>bold</strong> and <em>italic</em> text.");
    printf ("</body> </html>");
    return 0;

we had created the C file and time to compile now using GCC compiler.
change it to the present working directory for compiling.

$ cd <preferable directory>

compile and it should be successful compilation.

$ gcc file.c

just run your output and see .

$ ./a.out

have a look at my screenshot for visual reference.
after successful compilation now we are in the stage to create application/executable that runs on apache server in the cgi-bin directory.

$ gcc -o <your preferred executable name> <space><c program file name>

eg: $ gcc -o index file.c

now it's time to test your file using cgi-bin directory

$ sudo cp index /usr/lib/cgi-bin

this prompt for root password and copy the file to cgi-bin directory using sudo cpy command.

open your browser test your file as 

http:/localhost/cgi-bin/index   - awsome awaiting for you!

so the above method is some difficult to code huge html, i have decided to try file operation in c programming.


i have coded the normal <html> page and saved as input.txt file in my own dorectory.
next step is i had made the c program to load the input file and read the file line by line and create output as html tags.

have look at c program coding:

  int main()
            FILE *ptr_file;
            char blog[1000];
printf ("Content-Type: text/html\n\n");
            ptr_file =fopen("input.txt","r");
            if (!ptr_file)
                return 1;

            while (fgets(blog,1000, ptr_file)!=NULL)

            return 0;

the file operation opens the file input.txt in read mode and we have declared ptr_file as pointer and blog character array as 1000.since we are going to read the file line by line and just executing printf statement ,assuming that i dont have more than 1000 characters in single line.and followed by verification of file existance ans using while loop and condition is end of the line must have a null character or new line character and stopping there and thus moves to next line and proceeds till the file is completely read.And we close the file.

thus just code your input.txt file with html codings on your way.
my coded input.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shivasurya iniatives</title>
<style type="text/css">
.nav {
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none;
    background-color: #f2f2f2;
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc; }
.nav li {
    float: left; }
.nav li a {
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    color: #069;
    border-right: 1px solid #ccc; }
.nav li a:hover {
    color: #c00;
    background-color: #fff; }


<body style=" margin:0px;background-color:#FFE7E7;">
<div id="s_01" style=" height:120px; width:100%; background-color: #900;">
<img src="" style="margin-left:100px;"  />
<div id="s_02" style="width:100%;">
<ul class="nav" style="width:100%;"><li class="first leaf"><a href="/" title="" class="active" style="margin-left:110px; ">Home</a></li>
<li ><a href="/about" title="">About TCE</a></li>
<li><a href="/Academics" title="">Academics</a></li>
<li><a href="/admission" title="">Admission</a></li>
<li><a href="/departments" title="">Departments</a></li>
<li><a href="/Research" title="">Research</a></li>
<li><a href="/contact" title="">Contact Us</a></li>
<div id="main">
<img src="" style="width:100%; margin-top:-50px;" />

<div id="r1" style="height:auto; margin:20px;font-size:80%;">
<div id="m0" style="height:30px; width:350px;background-color: #FF5050;line-height: 30px;">&nbsp;&nbsp;&nbsp;<b>About</b></div>
<div id="m1" style="height:100px; width:350px;background-color: #FFADAD;">
<div style="margin-left:10px;">
<div style="height:33px;line-height: 33px;">
Founder: <b>Karumuttu Thiagarajan Chettiar</b>
<div style="height:33px;line-height: 33px;">
Located in <b> Thiruparankundram,Madurai</b> </div>
<div style="height:33px;line-height: 33px;">
Establishment Year: <b>1957 </b></div>
</html> <! shivasurya productions !>

and now create the application/executable file using compilation process.

gcc -o mainhtml file.c

now copy the both executable and input.txt file to cgi-bin using sudo commands.

$ sudo cp mainhtml input.txt /usr/lib/cgi-bin

verify using $ echo $?   must return 0 for success.

now it's time to check the website.go to http://localhost/cgi-bin/mainhtml

my output based on input.txt file :

using c program we could get query and combine database functionality using mysql C API.i will describe in detail in my next post.
feel free to share and comment guys!find out bugs if any an notify me via comments or contact by


Anonymous said...

Technically speaking, you didn't "create a website using C Programming Language," what you're doing is called Spaghetti/Macaroni code. You're creating a website with CGI/HTML; embedding HTML codes in a C program and having the program "spitting" it into a file or standard output is not "creating website using C PL," so convince yourself it is. Anyway, what's its use in production today, is the resulting code 2x faster than a conventional "write a html file, save it with a .html extension" way of creating a website?


so , as technically speaking you cant do database manipulation using html since it is a mark up language . and this is a start up i am going to introduce C API mysql
so i have given basic make up site using c lang and to proccess using db so it might be kidding for u ... c my future post ahaed. about mysql C API that connects Clang -HTML-Mysql Database

Shaikh Mohd Faizan said...

How to connect mysql database and execute query using C language

s.shivasurya said...
this would help you!
i would write tutorial soon on C language! subscribe for updates!

Post a Comment

feel free to post your comments! Don't Spam here!