How to Compile and Run CGI Scripts in C Examples in Linux Apache Server

How to Compile and Run CGI Scripts in C Examples in Linux Apache Server :-

first let us create an HTML file(testcgi.html) :
it can be made using keditor, geditor or vi editor


use form action http://localhost/cgi-bin/sum.cgi

then we have to make a C Program (sum.c):

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char *data;
long m,n;
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("Result\n");
printf("HTML Form values are added...\n");
data = getenv("QUERY_STRING");
if(data == NULL)
printf("Error! Error in passing data from form to script.");
else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
printf("Error! Invalid data. Data must be numeric.");
else
printf("The Sum of %ld and %ld is %ld.",m,n,m+n);
return 0;
}

To Compile:
in linux login root user and go to command line terminal
root# cc sum.c -o sum.cgi
root# chmod 777 sum.cgi
root# cp sum.cgi /var/www/cgi-bin/
otherwise find cgi-bin directory and put it there.
then put html file some where
root# cp testcgi.html /var/www/html/

next run apache web server:
root# service httpd restart

then open a browser and give an address like
http://localhost/cgi-bin/testcgi.html

Enter two numbers and the result will be displayed in the next page.