Monday, November 27, 2006

Why should you learn C?

Ok my previous post was not very constructive.

So I started off with the intention of posting a counter to my original argument. I was going to list the reasons why a programmer in 2006 should learn C. I was going to suggest that to be a C programmer you need to understand principles such as memory allocation and deallocation, pointers, creating string handling routines - basically all the things we dont need to worry about with languages like C# and Java.

Then I thought, why? Unless you intend to do embedded programming, really you DON'T need to know C. Even then you could probably use C# or Java, depending on the platform. C still has a place sure, but it's becoming like COBOL these days. Was great in the day, but has been superseded by newer, frankly easier to use languages.

Someone in the previous post suggested my arguments don't have enough substance, so I thought I would post some code examples of two basic programming functions in C and the equivalent in C# to highlight my issue with C. (My C is a little rusty so apologies if I have the function names wrong etc)

String Concatenation
C:

char * ReturnStringProduct(char * str1, char * str 2)
{
if (str1 == null !! str2 == null)
return null;
char * product;
product = (char *)malloc((strlen(str1) + strlen(str2) + 1) * sizeof(char));
strcpy(str3, str1);
strcat(str3, str2);
return product;
}

C#

private string ReturnStringProduct(string str1, string str2)
{
return str1 + str2;
}

Converting Int to a String
C:


There is a function called itoa, but I believe that it isn't in the ANSI standard so found a routine that did this on the net

void strreverse(char* begin, char* end) {
char aux;
while(end>begin)
aux=*end, *end--=*begin, *begin++=aux;

}

void itoa(int value, char* str, int base) {

static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";

char* wstr=str;

int sign;

div_t res;

// Validate base

if (base<2 || base>35){ *wstr='\0'; return; }
// Take care of sign

if ((sign=value) < 0) value = -value;

// Conversion. Number is reversed.

do {
res = div(value,base);
*wstr++ = num[res.rem];
}while(value=res.quot);
if(sign<0) *wstr++='-';
*wstr='\0';
// Reverse string

strreverse(str,wstr-1);
}

C#
intVariable.ToString();

4 comments:

Eat_My_Shortz said...

Hi. I think in the interests of being "constructive", I have to suggest that you (you = everyone) go and properly learn C. I started off perfectly happy in Visual Basic for 8 years. Then I learnt C.

But that's not enough. Just learning C may well confuse you as to why you had to know this stuff. But now that you know it, it gives you the ability to look deeper - to understand why your C# programs work. How the garbage collector works. How the OS works. Knowing this helps you write better C# code, just like knowing ASM helps you write better C code, and so on.

Having said this, I have to rebut your C examples.

Firstly, on the "int to string" function. The function you presented is actually more powerful than the C# version, because it converts numbers in any base.

However, if all you want is a decimal conversion, then C does have the function you're after - sprintf!

sprintf(targetString, "%d", intVariable);

Now onto string concatenation. It's true that in C you have to do more work in order to truly copy-concatenate strings (as opposed to just strcat). But why do you have to do this?

Because underlying the language C#, whenever you go strA + strB, it is allocating new space for them, copying A and then strcatting B. In C you do this yourself, in C#, it hides it from you.

This makes it easier to program... but a naive C# programmer has absolutely no idea what's going on. They could take 200 bytes and concatenate them one by one onto an ever-increasing string.

A C programmer knows not to do this, because they understand the underlying algorithms.

Most importantly: A C# programmer who knows C also knows not to do this.

Thus I submit to you, and everybody, use C# if you will, but learn C no matter what.

James said...

The thing some people seem to be missing is that I am not bagging C. I learnt programming using C 20 years ago. There would still be certain circumstances I would use C.

My point is, that you don't need to know C in 2006. Sure it will give you a good foundation, and all those points in the post above, but you simply don't need to know these things. You are not a bad programmer if you don't know C.

You are a bad programmer if you don't read about the language you are using and find out about the good points and bad points, and apply best practice.

The example above about String catting in C#. Any good C# programmer should know this, and now when to use strA + strB, and know when to use StringBuilder for performance reasons. These sort of things are well documented and you don't need to know C to understand them.

A good programmer will get to know the ins and outs of their language, read, watch and learn from experts in their field and continually to evolve their skills. All have absolutely nothing to do with C.

Eat_My_Shortz said...

There's only so much you can learn from a guide which says "Use StringBuilder if you plan to do many concatenations because otherwise the underlying system will reallocate the array every time you do it".

That makes it a rote fact that you need to learn. And every aspect of the language needs to be "learned" as "tips" or "facts".

The C programmer actually *knows* this for themself, because they actually understand what goes on underneath.

The C programmer is the one who wrote the guide. And the one who wrote C#.

James said...

Hmmmm, Anders background is in Pascal isn't it?