Pages

Sunday, February 14, 2010

Of Standards & Compilers

Pre/Post/Extra Marital affairs between  Standards and Compilers

The first thing we do when we start learning a new programming language, is to try and execute programs.
C is no different and for that, we need a compiler.

By popularity, we pick up a latest book, like Let us C ,and use the Turbo C compiler.

We might come across a code snippet somewhere, try to run it in Turbo C compiler, and get error messages.
We change the compiler, get a latest download,come across another code somewhere, try to run them, and get another error message again.  Although the code may be perfectly alright.

And being a beginner, we have no idea what is happening !
The book is wrong or their is a problem with the compiler ?

Here is something regarding that.

The C language was incepted in 1972. and Turbo C compiler was introduced in 1987.

Over time, the C language standards, provided by ANSI C committee,have undergone changes. From C89, to C90 to C99 , where the digits represets the year of their inception. And every standard introduced something new, modified something old.

And over time, some compiler vendors have managed to update themselves according to the standards. Some provided backward compatibilty, while some discontinued the technical support.

As a result we have numerous compilers in the market, each following some or the other standards. Creating a huge confusion. At the same time,none of them being faulty. <overlooking the bugs>.

Its important to understand the connection between them.

for eg.
The latest Trubo C (or Turbo C++) by Borland, is the 1990 version.Which means, it can implement only the C90 standard at most. The following code, when run under Turbo C, runs perfectly.

#include<iostream.h>   // header file to include input/output stream.

but the same code when run under Dev-C++ IDE using GCC compiler, much modern IDE and compiler, give errors.
Does that mean that gcc is a faulty compiler?  Definitely not.

The gcc compiler has the liberty to implement the C99 standards, which modified the header file syntax.
The above snippet, with little modification, will run perfectly in Dev C++.

#include<iostream>   // header file to include input/output stream.

but will give error in Turbo C/C++. Again, this doesnt mean that Turbo C/C++ is a faulty compiler. Just that it doesnt have the liberty to implement C99 standards.

For beginners, i will recommend that they use Dev C++ IDE, implementing gcc compiler and use a book written according to the new standards.


.........

< I do not mean to be biased towards other compilers like Borland 5.5 and MSVC++(beware, its microsofts), but DEV C++ is my personal favourite.>

For more info on the topic. use Google.

Have fun with C !

4 comments:

  1. Nice article, and I really like the name.. true we all must have encountered this problem..different sizes for same data types...unexplainable results after pre or post increment operators and stuff like that, the question is standardization really that important for C now?

    ReplyDelete
  2. its not that important. everything is implementation dependent. we just need to know the basic concepts of the language and take it from there, customizing as the need arises.

    ReplyDelete
  3. Cduced. The name is really cool and article is really nice.If this will continue,then you can write your own book.Cduced by Kaushik Mazumdar.

    The best thing i like about this article is the emphasis given to the basics of c which most of the people do not know esp. the beginners.

    ReplyDelete
  4. Nice blog brother...Ur article is showing ur good control over C nd C++ language.Few of ur stated problem, we also have encountered..Great job..

    ReplyDelete