10 Reasons - Why C Should be your First Programming Language

C/C++ 4 Comments »

by Anuj

To a beginner programmer the biggest question is where to start. Which language to choose from the mighty pool of 100’s of languages.

This is was the same question I asked myself when I started writing my first program. I tried many languages but finally I came to C, the most beautiful and charming language of all. I was literally blown away by the simplicity and elegance of C.

Though C is simple it is one of the most powerful languages ever created.

In this dynamic IT world new language come every day and get obsolete, so there must be something in the C which has remained there for 3 decades and more and even today there is hardly any language which can match its strength.

90% of the starting programmer says that C has been superseded by its predecessors such as C++, Java, and C # and so on so why learn C. I don’t know why they think so but I know one thing that they will never excel the other 10% programmers who differs from this opinion. Simple reason is how could a skyscraper building stand against time if its foundation is not strong.

C was the programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was written by a man name Dennis Ritchie.

Now Let us begin to analyze reason why C should be your first programming language.

1. I believe nobody can learn C++ or Java directly. To master these languages you need to have a strong concept of programming element such as polymorphism, classes, inheritance etc. Simple question is how you can learn such complicated concepts when you don’t even know about the basic elements such as block functions. C is a language which begins from scratch and it has foundational concepts on which today concepts stand on.

2. It is language on which C++ is based on, hence C# also derive its origin from the C. Java is also a distant cousin of C and share the same programming concept and syntax of C. These are the most dominant languages in the world and all are based on C. To rock the world through them you must get rocking with C.

3. C++, Java, and C # make use of OOP (Object Oriented Programming). Not all programs need it even though it is a powerful tool. Such programs are still written in C.

4. When ever it comes to performance (speed of execution), C is unbeatable.

5. Major parts of the Windows, Unix and Linux are still written in C. So if you want program these OS or create your own you need to know C.

6. Device drivers of new devices are always written in C. The reason is that C provides you access to the basic elements of the computer. It gives you direct access to memory of your CPU through pointers. It allows you to manipulate and play with bits and bytes.

7. Mobiles, Palmtops, PDA’s etc are gaining popularity every second. Also appliances such as T.V., Refrigerators, and Microwaves etc. are becoming an integral part of our daily needs. You may not know but they have a CPU with them which do need programming and the software’s written for them are known as embedded system programs. These programs have to be fast in execution but also have a very little memory. No question why C is ideally suited for embedded system programming.

8. You must have played games on your PC. Even today these astounding 3D games use C as their core. Why? The simple reason who will play the game when it takes a lot of time fire a bullet after you have given command from the console. The reply to the command should be damn prompt and fast. Reply in 1 Nano second is an outstanding game; Reply in 10 Nano seconds is crap. Even today there is no match for C.

9. C is a middle level language. There are three types of language – High Level, Middle Level & Low Level. High level languages are user oriented, giving faster development of programs, example is BASIC. Low level languages are machine oriented; they provide faster execution of programs. C is a middle level language because it combines the best part of high level language with low level language. It is both user and machine oriented and provides infinite possibilities.

10. Last but not least it is a block structured language. The first symbol of a modern language is that it is block structured. Each code exists in separate block and is not known to code in other block providing easy means of programming and minimizing the possibilities of undesirable side effects. C is designed from the base to top to be a block structured language. Many older languages, most popular being BASIC tried to introduce this concept but their short coming can never fulfilled as they were never built along these line.

I think I have given all reason I know why c should be your first programming language. One thing is for sure that there no other language which more reliable, simple and easy to use.

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

History of the C Programming Language

C/C++ 1 Comment »

by Adam McKerlie

The C programming language was first developed between 1969 and 1973 by a team from Bell Telephone Laboratories. Many of the principles and ideas used in this language were taken from the programming language named ‘B’ (created by Ken Thompson) as well as its ancestors BCPL and CPL (Combined Programming Language and Basic Combined Programming Language respectively). Dennis Ritchie was the main person responsible for converting the C language from B but there were many others that helped such as: Ken Thompson, Alan Snyder, Steven C. Johnson, and Michael Lesk.

People soon realized C’s power and flexibility. Because of this, the Unix operating system which was originally written in assembly language, was re-written in C. Because of this popularity many colleges and universities chose to adopt this new language because of its ties to Unix and the abundant availability of compilers.

By 1983 there were many different C compilers as well as many different interpretations of the language. This lead to portability issues (the ability to use the same code on different computer systems) and by the end of the year the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C. By 1989, the standard was ratified and was referred to as ANSI C, Standard C or C89.

In 1990, the ANSI C (with a few minor modifications) was adopted by the International Organization for Standardization (ISO) and became known as C90. Since then the only large revision to the standard was in 1999. This new revision was adopted by ANSI in March of 2000 and is know called C99. This is the most recent standardization of the C language and is the most commonly used.

While C hasn’t had any major revisions in almost eight years it has influenced many different languages. A few of the more notably languages include: C++, D and Objective-C.

The link included is a picture of the history of Programming.

http://www.levenez.com/lang/history.html

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

C++ - A Basic CGI Tutorial

C/C++ 3 Comments »

cgi and c++

Overview

This section describes the creation of CGI scripts in the C++ programming language. You should have prior knowledge of ANSI C++ before continuing. A number of excellent resources can be found in our CGI Links section.

That said, let’s get down to the nitty-gritty CGI internals! Making CGI scripts is actually quite simple if you know your way around C++. The server automatically takes whatever your program outputs to stdout and redirects it to the browser. So, for instance, if you wanted to send the text “Hello, World!” to the browser, you would simply state:

cout << "Hello, World!";

Easy, huh? Well, there are a few more things that have to be done…

The content-type header

Every CGI script must first output a content-type header that specifies the MIME type of the data that is being output by the script - for instance, text/plain if it is outputting plaintext, or text/html if it is outputting an HTML webpage.

The actual code for this header looks like this:

cout << "Content-type: text/plain" << endl << endl;

Note the two newlines following the header: this tells the server where the headers end and where the data begins.

Your first cgi script

Armed with this knowledge, we can create our first functional CGI script:

#include <iostream.h>

void main()
{
  cout << "Content-type: text/plain" << endl << endl
   << "Hello, World!";
}

Yes, believe it or not, this is a real, bona fide CGI script, even though it doesn’t do much. All it does, in fact, is sends the text “Hello, World!” to the browser.

Now, let’s spruce it up. Say we want to output not just “Hello, World!” in text, but why not make it pretty with some HTML formatting?

#include <iostream.h>

void main()
{
  cout << "Content-type: text/html" << endl << endl
   << "<html>" << endl
   << "<head>" << endl
   << "<title>CGI Test” << endl
   << “” << endl
   << “<body>” << endl
   << “<h1><em>” << endl
   << “Hello, World!” << endl
   << “

” << endl
   << “” << endl
   << ““;
}

Now we’ve outputted a real HTML page that the browser will recognize.

Using environment variables

Now, outputting an HTML page is all well and good - but why would you want to do so when you could just put the HTML page on the server? Well, here’s where the fact that you’re using C++ comes in handy.

When the server executes a CGI script, it first sets a number of environment variables that you can retrieve using the getenv() function. These will tell you a number of useful things. Have you ever seen one of those nifty pages that tells you your IP address?

#include <iostream.h>
#include <stdlib.h>

void main()
{
  char* lpszRemoteHost = getenv("REMOTE_HOST");

  cout << "Content-type: text/html" << endl << endl
   << "<html>" << endl
   << "<body>" << endl
   << "<p>" < endl
   << "Hello, "
   << lpszRemoteHost
   << "!" << endl
   << "

” << endl
   << “” << endl
   << ““;
}

You might also tell the user the current time, or even what web browser they’re using (stored in the HTTP_USER_AGENT environment variable).

Receiving data from a form

Okay, here’s where we get into what makes a CGI script really useful; receiving the data sent by a form on a webpage.

Using the POST method, a C++ CGI script retrieves the form data from stdin, the input buffer. The easiest way to do so is with the fread() function. There are a few things that have to be done first, however. You must retrieve the CONTENT_LENGTH environment variable, which tells you how much data is waiting. You must then allocate a buffer of this or greater length to hold the data.

Now, the data comes in a specific format. Each HTML <input> field and its value is sent along. Let’s say the following is the form in the webpage:

<form method="post" action="http://path.to/my/cgi/program">
<input type="hidden" name="value1" value="test1">
<input type="hidden" name="value2" value="test2">
<input type="hidden" name="value3" value="test3">
<input type="submit">


These are hidden input fields for the purpose of simplicity. The Webworks forms section in Advanced HTML will tell you how to make more complex forms.

Anyway, when the user submits this, this is what gets put into stdin for the script:

value1=test1&value2=test2&value3=test3

The CGI script is then responsible for parsing this into data that it can use.

Knowing all this, here’s a script that sends back what was sent to it:

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

void main()
{
  char* lpszContentLength = getenv("CONTENT_LENGTH");
  char* lpszBuffer;
  int nContentLength = atoi(lpszContentLength);

  lpszBuffer = malloc(lpszContentLength+1);  // allocate a buffer
  memset(lpszBuffer, 0, lpszContentLength+1);  // zero it out

  fread(lpszBuffer,1,lpszContentLength,stdin);  // get data

  cout << "Content-type: text/html" << endl << endl
   << "<html>" << endl
   << "<body>" << endl
   << "<p>" << endl
   << "Hello! You sent " << lpszContentLength << " bytes of data which read: <br>" << endl
   << lpszBuffer << endl
   << "

” << endl
   << “” << endl
   << ““;

  free(lpszBuffer);
}

Parsing the form data

You probably noticed that the server doesn’t do much to parse the form data for you; you have to do that yourself. It’s a very repetitive task, so I wrote a couple classes to do so. It’s too much code to reproduce here, but you can go ahead and download the source files:

* cgi.h
* cgi.cpp

These two classes, CCGI and CCGIItem, make it easy to parse the data.

Here’s how they work: you create a CCGI and a CCGIItem. Then you call CCGI::Load(), which reads in the data. You can then call CCGI::GetItemCount() to retrieve the number of items that were sent. You then pass CCGI::GetItem for however many items you want, passing a pointer to the CCGIItem which is filled with that item’s name and value. It’s quite easy to use, once you get used to it. Read through the source and you should get a better understanding of how it works (it’s fairly well commented).

There are many other libraries that provide much more complete CGI functionality; cgic and libcgi++ are two examples. You’ll find them in the CGI links section.

And here’s my final example, using my CGI classes:

#include <iostream.h>
#include <malloc.h>
#include "CGI.h"

void main()
{
  CCGI cgi;
  CCGIItem item;
  char* lpszOut;
  char* lpszContentLength = getenv("CONTENT_LENGTH");
  int i,nLength;

  cout << "Content-type: text/html" << endl << endl
   << "<html>" << endl
   << "<body>" << endl
   << "<p>You sent " << lpszContentLength

   << " bytes of data, which contained the following values: 

” << endl
   << “<ul>” << endl;

  cgi.Load();

  for(i=0; i<cgi.GetItemCount(); i++)
  {
    cgi.GetItem(i,&item);

    nLength = item.GetNameLength();
    lpszOut = (char*) malloc(nLength);
    item.GetName(lpszOut,nLength);

    cout << “<li>” << lpszOut << ” - “;

    free(lpszOut);

    nLength = item.GetLength();
    lpszOut = (char*) malloc(nLength);
    item.GetValue(lpszOut,nLength);

    cout << lpszOut << endl;

    free(lpszOut);
  }

  cout << “

” << endl
   << “” << endl
   << “” << endl;
}

You should now have the basic knowledge you need to go on and create useful & interactive CGI scripts in C++.

Source: ThinkQuest

Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.
WP Theme & Icons by N.Design Studio
Entries RSS Login