¤ Home » Programming » Developing Web Applications in C » Your first CGI Program

Your first CGI Program

The quickest way to learn the concept of CGI programming is to go ahead and write a few very simple programs, install them and see them run. This will enable you to quickly grasp the overall environment in which the CGI programs run. Hence, in this article and in subsequent articles, I will demonstrate some very simple CGI programs written in C and explain briefly the related concepts.

Write down this program yourself and install it on any web hosting server to see it in action. There are many free sites available which provide you free space to host your web pages. You can sign up with one of them to host your CGI programs and test them. Make sure that you enroll with a free site that provides a server space on the same operating system as the one you have on your own computer. For instance, if your computer runs Windows, get a space on a Windows based web server, so that when you compile your programs on your own computer, they will run on the web server. This is necessary because the free sites will not allow you to compile your C programs on their server.

Your first C Program

Here is the most basic C program that generates a web page to be delivered to any browser.

 #include <stdio.h>

 main(int argc, char *argv[])
	puts("Content-type: text/html\n\n");
	puts("<TITLE>Hello World!</TITLE>");
	puts("<h1>Hello World!!</h1>");
	puts("<p>This is my first CGI program in C.</p>");

Compile the above program on the same operating system on which it is going to run so that the binary code generated is compatible with the hosting server's operating system. After compilation the C compiler will generate the file helloworld.exe. Copy this file in the public_html/cgi-bin directory of your web server. Now, you can access this program from any browser by visiting your website with the URL http://www.yoursite.com/cgi-bin/helloworld.exe.

The output of the above program is shown below.

Note the first line of the C code - Content - type: text / html

All web pages generated by your C programs must begin with this header string as the first output. This is the Content-type header, which tells the web browser the MIME format of the output that follows, so that the browser knows how to display the content that follows.

Note: The 2 newline characters - \n\n, after the header string is important.

What is MIME?

MIME stands for Multi-purpose Internet Mail Extension, a specification that was originally developed for sending multiple types of data through electronic mail. MIME types are used to identify the type of data sent as content over the Web.

In our case, since the output is in HTML format, we are declaring the content type as text/html.

In the next few articles, I will take up more such simple CGI program examples and gradually build the concept of CGI programming.


comments powered by Disqus

Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy