¤ 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("<HTML>");
	puts("<HEAD>");
	puts("<TITLE>Hello World!</TITLE>");
	puts("</HEAD>");
	puts("<BODY>");
	puts("<h1>Hello World!!</h1>");
	puts("<p>This is my first CGI program in C.</p>");
	puts("</BODY>");
	puts("</HTML>");
 }


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.




Share:




comments powered by Disqus


Web Service Offers

Third Level Dot In Registration
599 199 (66.78% off)
per Year

.IN Registration
749 199 (73.43% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.





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