Bharat Banate's Work Profile

View Bharat Banate's profile on LinkedIn

Saturday, September 8, 2007

C: Is C under DOS the same as C under UNIX ?

Well, the topic seems to be weird... people can spend hours tyring to decide on this. There will be many people on either side, and none willing to budge.However, most of them will agree that in the end C remains a single entity no matter what the OS under which it is run.Why?The basic structure of C, its power, its data types, its control structures, its syntax all remain the same no matter which OS it runs under.So, where is the difference?
To prove a point to the non-believers lets write a program and run it under DOS and UNIX.


main (int argc, char* argv)
{
_int i;
_for(i=0; i < argc; i++)
_{
__printf("Argument is %s\n", argv[i]);
_}
}

Lets run this program with the parameter 'Hello World'.Under both DOS and UNIX two statements each will be displayed: the program name and 'Hello World'.
Now doesn't this prove that C under DOS and UNIX is the same?

No !!!
Lets run the program again, but with different parameter. Instead of 'Hello World', pass a * .
And what we get then?
Under DOS we get the program name and the star ' * '.
But under UNIX there is a difference. Instead of the star we get a directory listing.

There is the proof that C under these two OS's is different? Not so fast.
Lets first see what happened.Under DOS, C took the star as an argument and printed it. That's because the DOS shell, COMMAND.COM, took it for what it was: a star. The UNIX shell, however, is intelligent. It sometimes proceeds to deduce. In this case it interpreted the star to mean a directory listing. This directory listing was then passed to the program as an argument which proceeded to display the files on screen.

On the face of it, yes, the program worked differently and therefore we can say C is different under DOS and UNIX. But is it really the program that give us this difference in output? Not at all. It was the shell. If the shell had not been built to interpret the star, the directory listing would never have appeared. And that is hardly the fault of the program.

0 comments: