News for hackers. Stuff that matters.
Re:Object oriented programming with ANSI-C (1993) by mikevm on Saturday January 04, 2014 @02:25PM (#7012473)
When people tell me that they love C, I don't get it.

C is so tiny that if you're going to write anything non-trivial you either have to re-implement various common data-structures yourself, or find appropriate libraries. No built-in string type, no vectors, no nothing. Also, the lack of generics makes for really ugly code, filled with void pointers and casts whenever you attempt to write type agnostic data structures such as lists, queues, etc.

It also seems that whenever I have to write in C, I end up writing code that painfully wants to be object-oriented, or implementing features that are already part of C++.

So why would you willfully pick to use C when C++ is around? Heck, even if some sick reason you don't want to use explicitly use objects, at least you can enjoy some of the other benefits of C++ (e.g. its standard library).

Re:Object oriented programming with ANSI-C (1993) by georgemcbay on Saturday January 04, 2014 @03:19PM (#7012718)
"...or find appropriate libraries."

But... finding appropriate libraries is generally simple. Due to C++'s ongoing ABI issues C is, to this day, still the predominate language for library authors who want their library to be nearly universally consumable (in C, C++, Python, Go, Ruby, node/JavaScript, etc).

That said, I agree with part of your point which is why I use Go when I can today (though it also isn't suitable to be a C-like universal library language for other reasons). Built in strings, interfaces, maps, etc remove a lot of the basic code level pain points without also introducing the overbearing level of language complexity of C++/stl/boost (and I say that as someone who spent nearly 10 years programming professionally in C++ and at one time misguidedly considered myself a fan of the language).

Go is not so big on the full spectrum of traditional object orientation or generics, but once adapting to OO solely by composition and duck-type interfaces I haven't found either of those to be a big issue for me.

Re:Object oriented programming with ANSI-C (1993) by icedchai on Saturday January 04, 2014 @04:14PM (#7012963)
People love C because it's simple.

Figuring out what some C++ code does can be very complex, depending on what particular features the previous developers decided to use.

C is also the lowest common denominator, making it better for certain development tasks (I'm thinking of mainly lower level shared libraries.)

Re:Object oriented programming with ANSI-C (1993) by maratc on Saturday January 04, 2014 @04:15PM (#7012966)
Some people think that C is a better language than C++, and may disagree with you on standard library being a "benefit".

You may try to look at "C++ FQA" at http://yosefk.com/c++fqa/ for an example.

Re:Object oriented programming with ANSI-C (1993) by a8da6b0c91d on Saturday January 04, 2014 @08:34PM (#7013995)
If I recall correctly that FQA mostly bitches that C++ isn't Smalltalk, and offers no praise for C.
Re:Object oriented programming with ANSI-C (1993) by maratc on Sunday January 05, 2014 @03:51AM (#7015104)
No, FQA only mentions Smalltalk once in passing: "Lisp, Smalltalk and Forth are examples of languages with almost no syntax."

FQA mostly bitches about how C++ sucks, and comparisons to C are quite frequent.

Re:Object oriented programming with ANSI-C (1993) by EpicEng on Saturday January 04, 2014 @07:40PM (#7013800)

    It also seems that whenever I have to write in C, I end up writing code that painfully wants to be object-oriented, or implementing features that are already part of C++.
So I guess you just don't really know/aren't comfortable with C. That's fine, but that hardly means that everyone else has the same problem.

    So why would you willfully pick to use C when C++ is around?
Because they are two very different languages and have their own strengths and weaknesses?

OO is not the "one true way", that has been shown time and time again. It's really not difficult to find a library for things you need in C. The fact that they aren't all available to include the moment you open your editor/IDE isn't a reason to use C++.

I use both C and C++ regularly. I love a lot of things about C++, but it also brings a lot of baggage along with it. It is an extremely complex language and I have seen some really, terribly awful code written in it.

I've seen bad C code as well, but C++ makes it easier to write bad code and harder to restructure it if you need to because you have to deal with much more abstraction. C++ also lends itself to writing code which contains very subtle errors in it. C makes that more difficult.

I read your post and I just hear someone with little C experience who loves his objects and can't fathom doing things another way.

Re:Object oriented programming with ANSI-C (1993) by klipt on Sunday January 05, 2014 @02:25AM (#7014972)
> OO is not the "one true way"

Personally, I think C++'s big win over C++ is not OOP but generics. You can do OOP in C if you want; you can't do generics efficiently and safely.

Re:Object oriented programming with ANSI-C (1993) by dllthomas on Tuesday January 07, 2014 @05:39PM (#7020502)
You can't do generics in very much generality without compromising safety, true. You can get some (sometimes a surprising amount) done with macros, and safer now with static casts. Templates are still a win though, to be sure (syntax aside...)
Re:Object oriented programming with ANSI-C (1993) by EpicEng on Tuesday January 07, 2014 @07:55PM (#7021052)
Agreed, love templates.
Re:Object oriented programming with ANSI-C (1993) by 0x09 on Saturday January 04, 2014 @05:07PM (#7013175)
>C is so tiny that if you're going to write anything non-trivial you ...

pretty much know exactly how you're going to write it from the start. Since C is so constrained it's very easy to design for, and you usually have a very clear idea of what you are getting into, even if that includes reimplementing a common data structure or using external libraries. And if you are using an external library, the API is not likely to surprise you. There are countlessly fewer ways to design something within the rules of the language in C than C++. Different people will see this as a blessing or a curse.

For some situations it just makes sense. If your program revolves heavily around C libraries you will likely be forced into a C-like design anyway. And likewise, if you are designing a library with a primary interface in C. If you are already coding in this style, C offers some niceities to (ab)use that C++ doesn't (especially compound literals, designated initializers, and VLAs).

It's easy to become familiar with C which makes it easy for C to be a go-to language (obviously this depends on your problem space.)

Re:Object oriented programming with ANSI-C (1993) by aryastark on Saturday January 04, 2014 @06:54PM (#7013618)
> So why would you willfully pick to use C when C++ is around?

There are many reasons to not use C. But C++ existing is not one of them. Over the 15 years or so that I've used C++, there is always the constant nagging feeling that I don't really know what is going on. My programs tend to work. But I often find that there are incredibly subtle nuances buried deep in the C++ run time model that are simply beyond comprehension.

If you can keep all the rules of C++ in your head, then you are a god among gods.

Today there are few reasons to even need C++. I tend to stick with C or go to Perl/Python. I don't really need a language that attempts to straddle the middle. Especially one that more often than not, gets it all wrong anyway.

Re:Object oriented programming with ANSI-C (1993) by ketralnis on Saturday January 04, 2014 @04:36PM (#7013058)
And yet your operating system and over half of your userland is in C. Must be doing something right.
Re:Object oriented programming with ANSI-C (1993) by pjmlp on Saturday January 04, 2014 @06:31PM (#7013502)
Only if you are speaking about UNIX distributions, excluding Mac OS X.
Re:Object oriented programming with ANSI-C (1993) by EpicEng on Saturday January 04, 2014 @11:33PM (#7014547)
The Windows and OSX kernels are written in C. Not sure where you got your info.
Re:Object oriented programming with ANSI-C (1993) by pjmlp on Tuesday January 07, 2014 @02:14AM (#7016116)
Somehow the HN crash removed my comment.

The MacOS X drivers are written in C++.

Windows kernel code is being made C++ compliant given that C is a legacy language from Microsoft's point of view. Many new APIs since Windows XP are being made available only as COM objects.

As of Windows 8, the DDK officially supports C++ and many of the samples have been updated accordingly.

Re:Object oriented programming with ANSI-C (1993) by EpicEng on Tuesday January 07, 2014 @05:24PM (#7020442)
Yeah, the comments refuting what you said were as well.

OSX kernel is C. Drivers being written in C++ is irrelevant.

Windows kernel is C, the fact that they are making it compilable by a C++ compiler is irrelevant.

So what's your point exactly? Let us not forget you said that nothing outside *NIX was C, and you are wrong.

Re:Object oriented programming with ANSI-C (1993) by pjmlp on Wednesday January 08, 2014 @02:18AM (#7022215)
> OSX kernel is C. Drivers being written in C++ is irrelevant.

A kernel without drivers is useless.

> Windows kernel is C, the fact that they are making it compilable by a C++ compiler is irrelevant.

It is not irrelevant. Code compiled by a C++ compiler is C++, regardless how C fans try to bend it. Having this discussion since 1993.

Besides many new APIs since Windows XP are COM only as well.

C is legacy according to Microsoft, unless they change their mind about it. Even the latest C99 additions to their compiler were done as a requirement for C++11/14 compliance, not because they changed their mind.

> So what's your point exactly? Let us not forget you said that nothing outside *NIX was C, and you are wrong.

L4, Genode, OS/400, BeOS, Symbian are also done in C++.

What other OS, non UNIX based with the same maturity as the list above, can you point out being done in C?

UNIX based systems are the only ones that will be fully done in C due to their symbiotic relationship and the way their developers consider a sacrilege to use something else.

Re:Object oriented programming with ANSI-C (1993) by EpicEng on Wednesday January 08, 2014 @02:47PM (#7026018)
> A kernel without drivers is useless.

So... that means that the kernel is not written in C then? I'm confused.

> It is not irrelevant. Code compiled by a C++ compiler is C++, regardless how C fans try to bend it. Having this discussion since 1993.

Except they don't compile it with a C++ compiler, it simply can be compiled with one.

However, what would you call this, C or C++?

  #include <stdio.h>

  int main(int argc, char **argv)
  {
    char line[256];
    fgets(line, sizeof(line), stdin)
    puts(line);
    return 0;
  }
> What other OS, non UNIX based with the same maturity as the list above, can you point out being done in C?

Windows. Already did that. This discussion is now feeling circular.

Re:Object oriented programming with ANSI-C (1993) by pjmlp on Wednesday January 08, 2014 @03:35PM (#7026398)
> So... that means that the kernel is not written in C then? I'm confused.

It depends how important you consider the kernel, written in C, requires the drivers, written in C++, to perform any useful task.

> Except they don't compile it with a C++ compiler, it simply can be compiled with one.

Right now maybe not. Just wait when MSVC++ no longer does C.

> However, what would you call this, C or C++?

Actually,

It is C code according to ANSI C.

It is C++ code according to ANSI C++.

It is Objective-C code according to Objective-C language specification.

Re:Object oriented programming with ANSI-C (1993) by EpicEng on Wednesday January 08, 2014 @08:27PM (#7028149)
So... I guess I am confused. Here is how this has gone:

You: The only widely used, mature OS which is written in C is *NIX.

Me: No. The Windows and OSX kernels are written in C.

You: Yes, but OSX drivers are C++ and Windows is moving toward being able to be compiled by a C++ compiler.

Me: Right... but they are still written in C.

You: Yes, but that doesn't matter, you're wrong.

Me: ?

The discussion was centered around these kernels being written in C. They are, you have even conceded to that, yet you continue to argue (what point I don't even know) because OSX drivers are in C++ and Windows may some day in the theoretical future be compiled by a C++ compiler? I don't get it.

Re:Object oriented programming with ANSI-C (1993) by pjmlp on Thursday January 09, 2014 @02:02AM (#7029203)
So you don't get that a kernel without drivers cannot even boot. Drivers that are an integral part of the kernel, thus making an C/C++ hybrid, not pure C.

As for Windows, given the ongoing transition, and some of the chapters of Windows Internals book series, how can you prove that everything is still compiled with the C compiler and not C++ one?

You are right, it is silly to keep this discussion.

Re:Object oriented programming with ANSI-C (1993) by mratzloff on Saturday January 04, 2014 @06:30PM (#7013497)
Well, C is simpler than C++. That means it's simpler to understand exactly what is happening. It's not that far removed from an assembly language.

And you really can't beat C for FFI. If you have a common library you want to share among many different platforms, C is the way to go.

Re:Object oriented programming with ANSI-C (1993) by pjmlp on Saturday January 04, 2014 @04:14PM (#7012964)
I also don't get it.

When I got to learn C, back in 1992, I was already doing system programming in MS-DOS with Turbo Pascal.

A language with support for OOP, proper modules, blazing compile times, strings, vectors that don't decay into pointers, safer while offering the same low level features as C.

C looked stone age to me, luckily I got into C++ a few months later and saw a language which gave me back most of the features Turbo Pascal had, with the advantage of being a language in the process of being standardized.

I only used C when forced to do so during university assignments and on my first job.

Sadly C's compatibility is what helped C++ to become widespread, but also the cause of many of its issues in terms of security. At least with modern C++ is possible to avoid Cisms as much as possible.

Re:Object oriented programming with ANSI-C (1993) by to3m on Saturday January 04, 2014 @03:55PM (#7012884)
I've found this too. It's rather turned me off using C for anything; in no time at all, I always find myself doing something that would be very straightforward in C++, but is a certain amount of bother in C. So (I ask myself)... what is this buying me again? What's the point?

Fortunately, most C++ compilers' support for inline C code is excellent. Even gcc didn't get it wrong ;)

Re:Object oriented programming with ANSI-C (1993) by fatca on Saturday January 04, 2014 @09:15PM (#7014152)
In what are these various common data-structures supposed to be implemented? Also in a language that already has them?

We have to keep building and maintaining a kernel, a libc, and other basic infrastructure software. Should we also do that in a language that implements the missing things already? Maybe the CPU itself should implement these common data structures?

What solution do you propose?

Re:Object oriented programming with ANSI-C (1993) by EliRivers on Saturday January 04, 2014 @06:02PM (#7013369)
There are many platforms that have a C compiler, but not a C++ compiler. Sometimes you just don't have the choice.