LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2012, 07:37 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
Why c is still out there?


Hello everyone,
this is a serious question and I would like this to be a constructive post.

In the last month I was learning c again (which I like) but still some questions are all over my mind. I will just write few to trigger the discussion.

Please feel free to write and contribute to any direction you feel like.

Why we need C
1. Is it speed? How slower c++ or c# are?
2. Is it interoperability. Is c++ or java less portable between different os?
3. What makes it favorable and still used for server-based code. If I am not wrong apache,mysql, e.t.c are still written in (at least their core).
(feel free to also add your question).


Let's see what we will get from the forum's community.

I would like to thank you for your help

B.R
Alex
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-22-2012, 08:26 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by alaios View Post
Why c is still out there?
Ehm... because it's awesome? )

Quote:
Originally Posted by alaios View Post
1. Is it speed? How slower c++ or c# are?
2. Is it interoperability. Is c++ or java less portable between different os?
3. What makes it favorable and still used for server-based code. If I am not wrong apache,mysql, e.t.c are still written in (at least their core).
Following is just my personal opinion, so don't take me too seriously.

I don't think there's much difference in speed between C and C++. C is more or less a subset of C++ and the same code should be about as fast if compiled in C++ as it is in C. I don't work with C# so I can't compare that. The main difference is that in C++, you will usually use a totally different approach to solve the same problem, so, IMO, it is more a question of performance difference between programing paradigms than languages. C is a procedural language, while C++ is an (more or less) object-oriented language. Different programming paradigms are suitable for solving different kind of problems. While a GUI toolkit will be a good subject for OOP, taking advantage of features like inheritance, polymorphism, etc, a simple utility like cat, will hardly benefit from it.
Java is good for writing cross-platform applications, but since it runs in a virtual machine, it is not very fast. It is also not suitable for doing any platform/hardware specific stuff. You won't find many hardware drivers written in Java.
Every language has its place and its use. (except for FORTRAN which should be banned or something )

C is very fast, about that there is no doubt. It is not the fastest though, FORTRAN can often beat C in performance (usage of the restrict keyword in C99 can change the odds, but that's a story for another day).

C is also very powerful. It allows you to do a lot of dirty hacks. Be careful, though -- C is a good servant, but a bad master. (So is linux; with windows, it's the other way around )

C has a very good backwards compatibility. A C program written 20 years ago will usually compile and run nicely with a recent C compiler. C will also usually compile in C++, too.

Last edited by millgates; 05-22-2012 at 08:27 AM.
 
2 members found this post helpful.
Old 05-22-2012, 08:31 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
As soon as you have rewritten the entire Linux kernel in Java, let us know . . .

"C" is a high-level language that is designed to have low-level expressiveness: asm(...) anyone? It allows you to specify very exactly what you want the computer to do, yet it is transportable between many types of machines. Indeed, the Linux kernel source-code itself (especially in the /arch directory) is a textbook example of the power and suitability of "C" for its intended purposes.
 
3 members found this post helpful.
Old 05-22-2012, 08:35 AM   #4
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by alaios View Post
this is a serious question and I would like this to be a constructive post.
an unnecessary hint, but okay.

Quote:
Originally Posted by alaios View Post
Why we need C
1. Is it speed? How slower c++ or c# are?
2. Is it interoperability. Is c++ or java less portable between different os?
3. What makes it favorable and still used for server-based code. If I am not wrong apache,mysql, e.t.c are still written in (at least their core).
There's a number of reasons in favor of C.
  • History: C is one of the most widespread programming languages around. For projects that have used C for years, there's no point in jumping at something else.
  • Efficiency: C is -assuming a little practice- readable almost like clear text, and yet very close to actual machine code.
  • Availability: C compilers and toolchains are available for nearly any platform.
  • Simplicity: The language itself consists of just a one or two dozen keywords and a few syntactical structures. That's easy to learn. The power is in understanding them and combining them efficiently. Mastering the functions of the standard runtime libraries, however, is a different matter.
  • Ancestry: Many of today's programming languages have inherited some of their syntactical and logical structure from C. Therefore knowing C can make it easier to learn another language - like you have an advantage in learning a Romanic language (and allegedly even Russian) when you know Latin.

Though C can be used for nearly anything (even OOP is possible in plain C), it's quite sure not the best choice for all challenges. But it's a good nearly-all-purpose basis.

My favorite programming model is a mix of C and assembly language.

[X] Doc CPU
 
2 members found this post helpful.
Old 05-22-2012, 08:41 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
C is not only suitable for low-level code. Remember that GTK, OpenGL, SDL, and almost everything in Ryan Gordon's "Open Source Tools for Game Development" talk (video, slides) are C libraries.

Last edited by dugan; 05-22-2012 at 08:46 AM.
 
1 members found this post helpful.
Old 05-22-2012, 10:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Off-topic: Well, why is Cobol 'still out there'? Because it has applications that nobody wants to rewrite from crash.

Edit: Doc CPU has just warned me: the right word is 'scratch'... I hope I'll be forgiven...

Last edited by NevemTeve; 05-23-2012 at 01:55 AM.
 
Old 05-22-2012, 10:28 AM   #7
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by NevemTeve View Post
rewrite from crash.
was that an accident of yours? Or real intention? :-)
Usually, the phrase is "rewrite from scratch".

[X] Doc CPU
 
Old 05-22-2012, 10:52 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,892

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
from my point of view: if you know c very well you will be able to write quick and powerful code on almost every platform, you can keep also the [compiled] code relative small. Also you are able to implement almost everything, you are not restricted in any way. Using any object oriented language it is almost impossible: you can have nice code, but it will eat up much more memory and will work slower and also your access to the resources is limited.
From the other hand noone wants to write a kernel in fortran or perl.
 
1 members found this post helpful.
Old 05-22-2012, 11:19 AM   #9
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
The C "virtual machine" very closely matches native hardware, and so can be compiled to exactly machine code.

HOWEVER, do not confuse C with a 'low-level language'. It does NOT have a standard keyword for "use this assembly," as one poster implied. Additionally, there are things that cannot be done easily or conveniently in the language that are part and parcel of other languages. Ultimately, one of the major benefits of C is the access one has to actual memory addresses within the machine. Pointers are a double-edged sword, and provide a lot of great flexibility at the cost of a difficult concept to visualize (try nesting layers of pointers). Remember as well, you can write object oriented code in C, and procedural code in C++. OOP is more discipline than any particular language feature.

Other myths repeated in this thread are:

Quote:
Using any object oriented language it is almost impossible: you can have nice code, but it will eat up much more memory and will work slower and also your access to the resources is limited.
This is absurd, and false. I can write pretty C code. I can write pretty C++ code. I can even write pretty perl code. I can write a C program that is messy, full of leaks, and runs slower than equivalent java/perl/c#/c++/whatever. In fact, benchmarking folks that compare languages have some surprising results in the performance of equivalent java/c/c++/fortran code.

Quote:
Java is good for writing cross-platform applications, but since it runs in a virtual machine, it is not very fast
False. There are things virtual machines can do for speed boosts that compilers CANNOT DO in a standards conforming way. For example, Java VMs know which memory is most accessed, which branches are most used, which instructions are available on the target architecture, and where it can/cannot inline functions for optimal usage of the chip. Without non-standard compiler extensions (-march,-mtune anyone?), I can write you a series of programs that will perform abysmally in C/C++ when compared with Java and executed for a long period of time. VMs are not really as slow as they are purported to be. But look at some of the benchmarking numbers if you disbelieve.

Quote:
It is also not suitable for doing any platform/hardware specific stuff.
I'll refer you to JOS, JNode, and JX as a few examples where Java is able to implement hardware drivers. "Suitable" is usually a pejorative term for "can't do that" and in this case, it most certainly can. Two large companies even put significant R&D into them. What is your definition of "suitable"?

-Aaron
 
Old 05-22-2012, 12:34 PM   #10
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by orgcandman View Post
The C "virtual machine" very closely matches native hardware, and so can be compiled to exactly machine code.
OK. What is a "C virtual machine"?

Quote:
Originally Posted by orgcandman View Post
I can write a C program that is messy, full of leaks, and runs slower than equivalent java/perl/c#/c++/whatever.
Sure. You can write poor code in any language. That does not prove the language is slower.

Quote:
Originally Posted by orgcandman View Post
False. There are things virtual machines can do for speed boosts that compilers CANNOT DO in a standards conforming way. For example, Java VMs know which memory is most accessed, which branches are most used, which instructions are available on the target architecture, and where it can/cannot inline functions for optimal usage of the chip. Without non-standard compiler extensions (-march,-mtune anyone?), I can write you a series of programs that will perform abysmally in C/C++ when compared with Java and executed for a long period of time. VMs are not really as slow as they are purported to be.
Of course, each language performs well in what it was designed for and poorly in what it was not designed for. I do not deny that there may be cases where java may perform better then C. However, this is not the case in general. I agree that java is (slightly) faster than it would seem at first sight ("Knock knock! Who's there? ... [a very long pause] ... Java") but more often than not, C will outperform Java.


Quote:
Originally Posted by orgcandman View Post
But look at some of the benchmarking numbers if you disbelieve.
There are lies, damn lies and benchmarks.

Quote:
Originally Posted by orgcandman View Post
I'll refer you to JOS, JNode, and JX as a few examples where Java is able to implement hardware drivers.
"Suitable" is usually a pejorative term for "can't do that" and in this case, it most certainly can. Two large companies even put significant R&D into them.
I have never said that java can't do that. I only implied it is not suitable for the task. If I meant it can't do that I would have said so.

Quote:
Originally Posted by orgcandman View Post
What is your definition of "suitable"?
My definition of "not suitable" is "Not efficient at solving the given task" or "Requiring more effort while providing a less efficient solution". You can write drivers in java. You can write tetris in bash or awk. You can even port quake to excel. But there are more suitable tools.
 
Old 05-22-2012, 02:10 PM   #11
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
The one big difference between C and C++ not yet mentioned is that C++ has a significant runtime, whereas C has basically none.

Both C and C++ can be used in a freestanding environment; something like a kernel, which does not have access to the standard libraries at all, but has to provide some of the details itself. For C, this is really minimal: just a few types and macros and some trivial functions (like certain cases of integer division), which GCC and other C compilers provide already as built-ins. For C++, this includes exceptions and RTTI, which unfortunately require quite a lot of low-level implementation to handle correctly. I think one part, stack unwinding, even requires kernel support!

I do believe that the projects where C++ is stuffed into the Linux kernel specifically prohibits certain features like exceptions, to avoid the extra complications. I wonder if you can call an environment where some of the most basic features are prohibited, really C++? (Certainly not if you read the standards. But I digress.)

I'd like to clarify what I mean by runtime. Both C and C++ have standard libraries, which are usually linked dynamically. (It is easy to link C statically, yielding you a binary which is basically guaranteed to work in future Linuxes; some compiled for 0.99 still work in current kernels, according to Linus Torvalds. It is not that easy with C++.)

Most of the library contents for both are just functions and interfaces, code that is only run when the application explicitly calls them. By runtime, I refer to code that is run to provide functionality to the application, regardless of whether the application relies on it or not.

There is very little of that in C. Actually, I cannot really think of anything right now; everything I can think of is triggered by a standard library function call, is run at startup before main(), or at exit, when the process exits.

For C++, there is a lot of it. Exceptions at least require quite a bit of dynamic work for them to be possible. If garbage collection is added to the C++ library, that is another major part.

While the languages do have a lot in common, the runtime difference alone is large enough to consider them completely separate languages. The fact that they have common ancestry and share a lot (but not all) of syntax, does not eliminate their differences.

While I personally prefer C99 and GNU C over C++ any day, it is just because I need the control. The C++ runtime would get in my way. (Except, of course, if I were to write say an user interface, where the object orientedness of C++ is extremely useful, and makes it much cleaner. That said, GTK+ is not too shabby either, even if it is written in C.)

I just get very annoyed when C and C++ are considered "almost same". They're not.

Finally, I'd like to point out that because of human tendency to think inside the box (as opposed to outside it), having multiple languages, especially ones with totally different approaches, is very important. Personally, I only truly started to realize the fundamental power of algorithms (as opposed to their implementations) after I learned my third or fourth programming language well enough for commercial work. As the old saying goes: If the only tool you have is a hammer, all your problems start looking like nails.

In that sense, the OP's original question could be paraphrased as "why do we waste steel and other metals by producing screws, when we have plastics and super adhesives?"
 
3 members found this post helpful.
Old 05-22-2012, 03:59 PM   #12
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by Nominal Animal View Post
The one big difference between C and C++ not yet mentioned is that C++ has a significant runtime, whereas C has basically none.
at that point, I felt the impulse to argue, because I didn't see yet that you distinguish between a runtime and the standard libraries - because for me, that's the same. But I continued reading first.

Quote:
Originally Posted by Nominal Animal View Post
Both C and C++ have standard libraries, which are usually linked dynamically. (It is easy to link C statically, yielding you a binary which is basically guaranteed to work in future Linuxes; some compiled for 0.99 still work in current kernels, according to Linus Torvalds. It is not that easy with C++.)
I'd even say that static linking is the more typical procedure with C - the very standard libraries are so small that every binary should have them in its own baggage, rather than relying on them being present as a shared library. Assume as little as possible.

Quote:
Originally Posted by Nominal Animal View Post
Most of the library contents for both are just functions and interfaces, code that is only run when the application explicitly calls them. By runtime, I refer to code that is run to provide functionality to the application, regardless of whether the application relies on it or not.
Then you call runtime what I'd rather call undesired ballast.
No matter what we talk about - electronic gadgets, software, cars, power tools, employees, service providers: I like them to be able to do as much as possible for me, but nothing that I didn't explicitly ask for. And I'd like to have it the same way with programming languages.
C matches this ideal almost perfectly. It's relatively easy to write and to develop, and it does exactly what's written in the source. C++, on the other hand, has a lot of magic going on behind the scenes - its own memory management, automatic calling of constructors and destructors, garbage collection. All these are things I'd like to take care of myself.

Quote:
Originally Posted by Nominal Animal View Post
There is very little of that in C. Actually, I cannot really think of anything right now; everything I can think of is triggered by a standard library function call, is run at startup before main(), or at exit, when the process exits.
Yes. Great, isn't it? A system that does what you tell it, but nothing more.

Quote:
Originally Posted by Nominal Animal View Post
While I personally prefer C99 and GNU C over C++ any day, it is just because I need the control. The C++ runtime would get in my way.
Same here. I don't need the control, actually, but I urgently want it. It's my nature.

Quote:
Originally Posted by Nominal Animal View Post
I just get very annoyed when C and C++ are considered "almost same". They're not.
Of course they're not. C++ is the result of C growing rampant. Or, more soberly, C is the basis that C++ was developed from.

Quote:
Originally Posted by Nominal Animal View Post
In that sense, the OP's original question could be paraphrased as "why do we waste steel and other metals by producing screws, when we have plastics and super adhesives?"
Very aptly put, I think. :-)

[X] Doc CPU
 
Old 05-22-2012, 04:01 PM   #13
nixblog
Member
 
Registered: May 2012
Posts: 426

Rep: Reputation: 53
Quote:
Originally Posted by NevemTeve View Post
Off-topic: Well, why is Cobol 'still out there'? Because it has applications that nobody wants to rewrite from crash.
It's still out there for COBOL programmers to make lots of $$$
 
Old 05-22-2012, 04:04 PM   #14
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Nominal Animal's reply was quite illustrative! I learned a lot from it.

A good reason (a personal opinion) could also be that C syntax is highly intuitive, from a mathematical point of view, even though it was not originally conceptualized for numerical or scientific computing, or at least not in the way FORTRAN was. I mean, take a look at how function calls are performed:
Code:
yy = ff(xx);
and compare to a canonical FORTRAN function call. This is mathematically intuitive!

My point is that, C is NOT only a strong language with a lot of capabilities BUT it is also a really intuitive one... recall that Java designers decided to inherit C's syntax.

It was my personal experience that I learned C as my first programming language, and I'm glad I did!

Notice also, that a arising technologies such as CUDA stand on the shoulders of C!

Funny note: Look for the definition of C in the Glossary provided in [1]!

Last edited by ejspeiro; 05-22-2012 at 04:08 PM. Reason: Spelling.
 
Old 05-22-2012, 05:09 PM   #15
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by ejspeiro View Post
It was my personal experience that I learned C as my first programming language, and I'm glad I did!
That was different for me. As a teenager, I learned BASIC as my first programming language, as the early 1980's were just the time when home computers became popular - most of them intended to be programmed in BASIC. I was very soon bored with BASIC and dug into assembly language for the Z80 and the 6502 processors at almost the same time. I never got to like the Z80, but made friends with the 6502 at once.

The next programming language I learned was Pascal. My IT study curriculum had scheduled this educationally brilliant language as the first language an IT student should get to know. Hmpf. Not that I disliked Pascal, but I felt very constrained by that language.

When they began with C a year later, I briefly felt mystified at the cryptic syntax: C often uses one single symbol where Pascal has a whole word. But very soon I found that C eventually let me do all the things that Pascal wouldn't, but which had become an essential part of programming for me in assembly language programming. Like forced typecasts even when the compiler "thinks" the types are incompatible. Like a switch statements with deliberately unterminated cases. Like bit operations and bit shifts. Like smartly nested expressions. Finally freedom of coding again. Then, later, some C++, a brief glance (but not more) at Java.

Many years later, I added Javascript and PHP for the web context. Both very specialized languages with their own pros and cons, but both very much based on C syntax and logics.

[X] Doc CPU
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration