Sign in to follow this  
johnnyBravo

Why do people still use C ?

Recommended Posts

Hi, I was quite curious why people still use C, if C++ is just as fast , has backwards compatibility and has OOP capabilities. As at my friend's uni, the are currently teaching them that, im not sure what course he's doing, ...forgot to ask. Also apart from updating existing c programs. Thanks

Share this post


Link to post
Share on other sites
Your friend is probably being taught that because C programs are programmed differently than OO C++ programs. It's best to have experience with a wide range of programming paradigms.

Share this post


Link to post
Share on other sites
Some reasons to use C are:
- Existing applications
- The platform you are developing for has no C++ compiler (embeded systems)
- You don't need te features of C++, since you need maximum speed. Low level code, like drivers, operating system stuff...

Share this post


Link to post
Share on other sites
Some people simply like C more than C++. They do usually use a modern C++ compiler to code with though, which is backwards compatible as you said.

If I'm writing a small program I usually code in classless C++, using just functions but making use of features of the C++ language like new/delete. If I'm writing a larger program I use OO code.

Share this post


Link to post
Share on other sites
I would say it's because no matter what platform you have to work on, there is most likely a C compiler for it. The C runtime library is small, fast, and relatively easy to implement compared to the C++ runtime library. Often times the first thing done for a new platform is writing a C compiler and runtime for it.

C is good to know so that one day when you have to work on platform XYZ that doesn't have a C++/"insert favorite language here" compiler, you can be pretty sure it has a C compiler.

Share this post


Link to post
Share on other sites
If you us ANSI C it is much easier to port your application to other platforms, because (as was said earlier) most platforms have a C compiler. Also some people (myself included) prefer structured programming as opposed to OO. At work I use C to program for an embedded system.

Share this post


Link to post
Share on other sites
C is not often used by new programmers or many hobbyists because it requires dicipline, and that's no fun! ;-)

But once writing code is about getting the code written, C is

1. Clean. Super clean. Diciplined C code is a dream to read.
2. Fast. It's C, after all.
3. Portable.
4. can be as abstract as necessary.

So basically, a lot of people look at what they want to write, decide that it need to be lean and mean, and so dismiss things like python, vb, c#, whatever, and then choose C by default.

Why not C++? Sure, it's fast ("only pay for what you use"). It's only somewhat less portable. But the abstraction and clean parts get messed up.

Huh? abstraction in an OOP languages being worse than a non-OOP language?

Yeah. OOP is sometimes a square peg for a round hole. Sure, C++ lets you solve those problems by using a different style, but suddenly you loose all that syntax sugar C++ has for OOP and you're left with horribly ugly code. Just look at some of the template functional programming garbage. Horrendously complex.

And it's not like you can't write OO code in C. and when you do, its style is more consistent with other programming styles.

Share this post


Link to post
Share on other sites
a lot of the OOP in C++ is done during pre-processing. Classes end up getting converted to regular C functions and structures during compile time. It's not like C++ is going to be faster. The real difference is that C++ may to some people feel more user friendly and they may like using it. C++ simply adds a *this pointer parameter to functions of classes and passes *this pointer for you ... This can be achieved in C just as well but will not look or act as pretty.

picture this:

c
rotate(theobject,x,y,z)

c++
theobject.rotate(x,y,z)
or
theobject->rotate(x,y,z)

Really their is no difference at the end of the day. If you are looking for a language that masks the underlying code from you to help you program better, then you should look into Java or .Net languages, because in general thats all they do.

Share this post


Link to post
Share on other sites
As it happens, I remember I've always liked using C libraries better than the C++ ones (OpenGL vs. DirectX, SDL vs. CDX, and others). The very same goes for my own modules.

Portability (between platforms and compilers) and compiling speed are some other advantages.

The biggest reward for not going C++ has been that it is vastly easier to connect a C program to other languages than it is for a C++ one. I try to move away from C as well, but C++ is not where I want to end up. I need a dynamically typed language, like Lua or Lisp. (Lua by the way is coded as a vanilla C library.)

Share this post


Link to post
Share on other sites
Some of us are just stuck in our ways :) I've been coding in C for over a decade and never saw a great immediate advantage in switching to C++. OOP techniques may be a little clumsy since they're not integral to C (you have to "roll your own" implementations), but to say that's a reason to go C++ is like saying the pronunciation rules of English are so hazy that everyone should switch to German. Use the language you know and what gets the work done.

(Yeah, having SDL/OpenGL based code that compiles with no modifications on win32/mac/linux is a good thing too)

Edit: The "C++" style in Codemonger's example is just as good in C. I use function pointers in structs all the time. Typically a game entity is a struct with pointers to update/think/interact style functions. You see this a lot in Quake-based code too.

Share this post


Link to post
Share on other sites
I usually prefer C libraries to C++ ones too. Because of the many different ways you can use OOP, some libraries can be confusing or different to how the end user would code. C functions are pretty self-explanatory though, you pass values and take a result, and the function does what it says it does.

In my current engine I encapsulate SDL and other C libraries inside a DLL, so that I get the benefits of only graphics code being able to call graphics functions, without having the added complexity of using other peoples' classes.

Share this post


Link to post
Share on other sites
C++ sometimes seems like a big kludge that tries to force the last two decades buzzwords together into a single language.
C is minimalistic and almost every feature is cleanly integrated into the language, yet there's very little it can't do.

There's just something elegant about it. And minimalism has always been attractive to coders.

Share this post


Link to post
Share on other sites
The problem I see is that most C nerds think they're the one person who won't make a mistake. They're the one person who won't have memory leaks, threading access violations, deadlocks, buffer-overrun vulnerabilities, uninitialised pointers, undecipherable code, etc.

You are not a special and unique snowflake, and I'm sick of dealing with your code.

C is for hardware interfacing, legacy, and personal projects that nobody else needs to use. Otherwise, do not use it. And using C++ but only calling C functions is not a solution - its just as bad.

C++ is an overcomplex kludge that converts C into an OOP language. But its also the fastest popular OOP language, so it wins.

C is like goto. Its an unsafe anachronism that should be left to the hardware nuts.

Share this post


Link to post
Share on other sites
C is for embedded systems, low level systems and kernel code, etc. It's fantastically portable, small, and efficient. It is suitable and in fact ideal in those situations.

However, seeing major applications and such coded in C now just irks me. It causes more problems than it solves. So Raduprv and the OSS world can bite me [lol]

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
But its also the fastest popular OOP language, so it wins.


There is more to programming than OOP. C++ is a hack, it is far more difficult to write portable standard compliant C++ code than C.

As has been mentioned, C's beauty is in its simplicity.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
The problem I see is that most C nerds think they're the one person who won't make a mistake. They're the one person who won't have memory leaks, threading access violations, deadlocks, buffer-overrun vulnerabilities, uninitialised pointers, undecipherable code, etc.

You are not a special and unique snowflake, and I'm sick of dealing with your code.

C is for hardware interfacing, legacy, and personal projects that nobody else needs to use. Otherwise, do not use it. And using C++ but only calling C functions is not a solution - its just as bad.

C++ is an overcomplex kludge that converts C into an OOP language. But its also the fastest popular OOP language, so it wins.

C is like goto. Its an unsafe anachronism that should be left to the hardware nuts.



To me it seems like you misunderstand C, or you don't understand programming languages in general. Saying C is like goto is just silly, considering all your precious C++ code (ASM) internally uses nothing much different using labels and returns. I like the control C has and offers. I personally don't care for the overhead and backage that C++ has for a OOP language.

You are right that C++ is very fast when compared to other OOP languages, but its really not as clean as other OOP languages. It almost seems like a hacked up version of "C". C++ is no different than using Macro's for you to more easily understand your code, that's all.

You would probably like to think that your Polymorphing, Overloading C++ stuff are actually part of the language, when really they are no different than macro's in a general sense. Sure their handy but don't fool yourself and get caught up in buzzwords, when really its just rearranging your code or adding extra code without you knowing.It's C and ASM in the end anyway.

As far as C++ converting C into OOP ... they did a bad job that's all, because it's damn messy. Check out "D" language, it's nice and clean or Java or C# or VB.NET for that matter.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
The problem I see is that most C nerds think they're the one person who won't make a mistake.
And the problem I see with most C-bashing nerds is that they think using other languages makes them safe from ever writing flawed code.

Disciplined C coders will write code that makes none of the classical mistakes. It's far easier than anyone seems to think. C's flaws are known quantities.

Can you even come up with best practices and things to avoid in C#? Do you even know what code you write might end up being a security flaw?

People talk about how C++ coders should be using std::string and the like to avoid buffer overflows and such. The same principle still applies to C programmers: use a library that's got fixed functions.

Share this post


Link to post
Share on other sites
I'm well aware of all of these things - the Python interpreter (which I've tinkered with extensively under the hood) is an excellent example of what can be implemented in C. Python is a garbage-collected OOP interpreted language, and underneath its all C code.

The point is that its easy to forget one little thing. All the reference counting on the C side is manual - forget to incref or decref, you screw up. Let a NULL pointer into a PyObject, you'll crash the interpreter. In C++ you can make these compile-time concepts. And its not like Java, where you have no choice as to what happens at compile time - you can control it in C++. C++ is just a very well defined, compiler-protected set of #defines, really - but that's why its good, not why its bad. You can write clean, OOP code, but you can also optimise that same code as well as your old C. That's the point - its possible to make dementedly optimised but also very legible C++. Meanwhile, optimising in C necessarily introduces illegibility, as you have to expose more control to the code to allow for optimisations, rather than encapsulation.

And anyways - the point is that you may think that your C code is better, but with a good use of the STL and avoiding pointers you're much safer from buffer overruns, dangling pointers, thread safety violations, memory leaks, and the many other plagues of C. Yes, careful, judicious coding is just as good - but if you think that's a safe bet then you're living in a dream world.

Code first, optimise later. In C++, you can code first in high-level oop, and then get your hands dirty popping things open and getting hardcore procedural on them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
They're the one person who won't have memory leaks, threading access violations, deadlocks, buffer-overrun vulnerabilities, uninitialised pointers, undecipherable code, etc.


I am sorry, which of the above issues are corrected by C++? Ooops. Fundamentally flawed argument.

Quote:

You are not a special and unique snowflake, and I'm sick of dealing with your code.


That's nice. Have a cookie. I am sure that someone deliberately went out of their way to write ugly code to annoy you. It couldn't possibly have anything to do with writing extremely portable code. I personally think that the code in GNU's getopt.h & getopt.c is really ugly, but you know what kicks? The fact that I can seamlessly recompile it in C or C++ on FreeBSD, Linux, MacOS X, and Windows and not have to rewrite my code for handling command line parameters.

Quote:

C is for hardware interfacing, legacy, and personal projects that nobody else needs to use. Otherwise, do not use it. And using C++ but only calling C functions is not a solution - its just as bad.


C is a tool, just like C++, or Java, C#, or even Visual Basic. Each has a place where they provide a better solution; to believe otherwise is to willingly wrap yourself in a cloak of ignorance.

Quote:

C++ is an overcomplex kludge that converts C into an OOP language. But its also the fastest popular OOP language, so it wins.


Umm okay, it may execute the fastest, but in terms of development time? This is why I have been using Microsoft .NET and Mono.NET more and more lately [as satan chuckles in the background]. One of my co-workers who happens to be a kernel developer for FreeBSD is planning to implement an image loader for the .NET framework as a kernel option to optimize the performance of .NET on one of our web servers.

Oh, did I mention that he programs primarily in C? I guess it must be that he agrees in using the right tool for the job.

Quote:

C is like goto. Its an unsafe anachronism that should be left to the hardware nuts.


C is like a hammer. Its not subtle, and it gets the job done. If you don't hit the nail squarely on the head then you are bound to screw up the job. If you don't like it get a nail gun or hire a professional.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why does a dog lick his balls?

because he can.

Share this post


Link to post
Share on other sites
Point is C++ is by no means flawless, and to try to redirect a noob into using C++ instead of C because it's somehow easier than C or manages memory better, is just dumb.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pxtl
The point is that its easy to forget one little thing.

That's true... to a point.

With C, since the language easily lets you screw yourself, you have to be disciplined. (how many times have I said this now? am I starting to sound like W debating?)

People learn languages easily enough, and with some languages you can get by with only learning them. With C you have to learn the language AND the best practices, otherwise you end up with shitty code like you state.

But once you know both, buffer overflows disappear in secure library functions, and memory management becomes automatic because you DON'T forget to free something.

If you're writing C code and it's hard to keep track of memory management, you're not writing it correctly. A memory leak is not a sign of a bug, it is a sign of a stupid programmer or a bad design.

C still has appeal because there ARE programmers that know C, and know how to write C, and are good at designing C code.

Anyway, this isn't a thread about whether or not you think C sucks, it's about why C is still used. I think that's been answered.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codemonger
Point is C++ is by no means flawless, and to try to redirect a noob into using C++ instead of C because it's somehow easier than C or manages memory better, is just dumb.
Well, they should really be directed away from both.

C++ is just too complicated, and C requires discipline (again! ug!) that would need to be taught simultaneously.

That makes both hard to teach.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this