views:

971

answers:

16

Is it possible to write in C programming language everything that you could write in other languages such as Java, C# or C++. If that's the case why don't schools these days teach C instead of Java?

Ok the main reason why I am asking is because I don't want to tie down to a single programming language and platform (.NET and C# or Obj-C and Cocoa). Perhaps I am confusing a programming language with a framework? If anyone could clarify all this for me, I'd certainly vote for your answer.

+5  A: 

Yes. You can do anything you want in any language you want (well, almost). Sometimes the frameworks that make your life easier are easier to use from one language or another, but the computer sure doesn't care what language you programmed it in. There are always exceptions, of course.

Carl Norum
Well, C doesn't have exceptions ;)
nall
@nall, unless you write them... I mean, the compilers for all the languages the OP mentioned were all written in C to begin with, so at the *very* outside, you can still write a compiler that supports exceptions and then use it....
Carl Norum
@nall, fyi C do have exception.
YeenFei
@YeenFei: C++ has exceptions at the language level, Windows has exceptions at the OS level, and you can use an exception library for C (see for example http://www.on-time.com/ddj0011.htm or http://www.nicemice.net/cexcept/) but the C language in and of itself does not have exceptions.
Timbo
+31  A: 

Any language which is Turing Complete, i.e. C, C++, Java, and C#, can calculate anything that is computable.

Billy ONeal
@Eli Courtwright: Thank you :)
Billy ONeal
Go, brainf**k !
Vuntic
As @Vuntic says, this answer is strictly true but not really helpful. There are many things we use programming languages for, which are not computation. For example, you can't write a device driver in a language whose only concept of I/O is stdin/stdout, not because it lacks Turing power, but because you can't interface it in relevant ways.
Amadan
@Amadan: A nebulous question like this one deserves an equally nebulous answer. I actually didn't intend this answer to be all that useful and am genuinely surprised that so many upvoted it LOL!
Billy ONeal
If you're going to invoke Turing completeness, you might as well take the position that all languages are the same and any one is just as good as any of the others. I, too, am surprised at the number of upvotes.
Norman Ramsey
@Norman I'm not surprised. Many folks on SO read something like `Turing Complete` or `Early optimization is the root of all evil` or `Jon Skeet` and automagically click upvote.
Tom
I chose not select this as the best answer because I've never even heard of Turing Complete term before. So it wasn't much of a help to me. But thanks anyway ONeal.
Silence of 2012
@Tom I love it! From now on I will find the way to include the magic words `Jon Skeet` in every answer...
Norman Ramsey
@Tom am gonna sign off all posts with "early optimization is the..
Amarghosh
@Tom: Was that comment an attempt to demonstrate?
Billy ONeal
@Norman Ramsey: The OP didn't ask what was *practical* to implement in a given language; he asked what was *possible*. Turing completeness allows you to do anything that is *possible*, though not necessarily *practical*.
Billy ONeal
@Billy Turing completeness applies only to run-time extensional properties of programs (do they terminate and if so with what value/effects). It says *nothing* about compile-time checking (a static property) or memory leaks (an intensional property). Both these properties are quite important in real programs. I don't see that the question is so narrowly phrased as to rule out intensional properties.
Norman Ramsey
@Norman Ramsey: I agree they are important in real world programs. But the question doesn't say anything about that. It asks only if creation of such a thing is possible. It doesn't ask which is better, or why, or anything like that. (To be fair, the question is extremely poorly written)
Billy ONeal
@Tom: Clearly the best course of action is to invent a Turing tarpit language, name it "JonSkeet", then use it as an example to discuss optimization. The upvotes will practically click themselves!
camccann
+3  A: 

Generally speaking, yes, you can make comparable programs in just about any programming language (assuming it has the right libraries for your purpose, e.g. windowing, etc).

However, each language has its own strengths and weaknesses, which is exactly why Java is the predominant teaching language. Java is higher-level than C so it frees the developer from having to do difficult tasks which can be automated (such as memory management) allowing him to focus on other things, like algorithms, which is probably why it's so common for teaching. However, with that advantage comes penalties, such as performance and memory consumption, which are areas in which C can be very efficient.

maerics
Thanks that clears up my debate of 'Java' vs 'C' for teaching purposes :)
Silence of 2012
Just wanted to add that any decent school will have classes taught using multiple programming languages. Being able to recognize the right tool for the job is a very important skill, and experience in multiple programming styles helps you think about problems in multiple ways.
Mark Bessey
I think that isn't quite why Java is the predominant teaching language. Lisp, Perl, and Python (among others) had all that long before Java came around.
Chris Lutz
@Chris, it's true, garbage collection is just a single feature of many that makes Java preferred for teaching; I suspect the strongest reason now is that Java is very common in the industry and more universities are under pressure to teach vocational skills as well as academia.
maerics
@maerics: I would disagree that Java is highly used in industry. Most of the time I see either C++ or C#. (To be fair, C# is almost exactly the same experience as Java to the developer)
Billy ONeal
+6  A: 

You could probably "achieve" the same functionality using only the C language when developing an application, but the effort required vs. more modern object-oriented languages would probably be significantly higher. Everything eventually does compile down to machine language. Also, the ability to write applications using design patterns is probably significantly diminished using the C language.

The richness of the communities surrounding modern language on the internet is also a huge value-add when deciding on which language to use. The Open-Source Software projects, the forums and sites dedicated to improving the life of developers and the quality of applications is really vibrant around modern languages.

This also doesn't really speak to the huge value that the run-times of the various modern languages provide to make developers lives easier.

These are just a few points that I could point out.

Cheers

Dave

Dave White
"Also, the ability to write applications using design patterns is probably significantly diminished using the C language." Bah. Object-oriented != organized. You can have one without the other (in both ways).
Chris Lutz
You seem to allude to C not having a vibrant open source community. I refer you to Linux... and 16.8% of Sourceforge (Java has 25.4%, C++ 20.5%). Source: http://holycall.tistory.com/entry/SourceForge-Programming-Languages-Statistics
Stephen
@Stephen: those stats are from 2008. I don't think they are that relevant anymore. Also, the OP was talking more about C as distinct to C++.
Craig T
@Craig T. I too was referring to C as distinct from C++. And, yes, it has been 18m since those stats were published, and I'm sure it's diminished... but I'll guarantee that it still makes up a large portion of the open source listing. Look at github, C appears to edge out both Java and C++ in number of projects as recently as Dec 2009 ( http://corte.si/posts/code/devsurvey/index.html ). And another count puts it at number one in number of bytes stored on github ( http://gist.github.com/118810 )
Stephen
@Stephen, @Craig T: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
afriza
@ChrisLutz - All of the literature that I've read regarding design patterns uses object oriented features for their implementation. I'm not saying that there are no patterns in C or that C code is un-organized.@Stephen - My comment was pointing out that the communities surrounding modern OO languages is rich. Not that there are no communities surrounding C or that there are no C open source projects.@Afriza - C makes up ~18% while OO languages (Java, C++, C#) make up ~32%
Dave White
+4  A: 

Is it possible to write in C programming language everything that you could write in other languages such as Java, C# or C++.

Yes and no.

Yes, in theory: Because using C, you can write Java, C#, and C++ compilers and run time environments (and then write anything that you can write in Java, C#, and C++)

No, in practice: Because Java, C#, and C++ already have run-time libraries which don't exist for and aren't easily callable from C; and because Java, C#, and C++ provide many high-level features (e.g. for memory management and OOP) which make programming easier ... and if you're programming at the limit of your ability, then a language which makes programming easier means that you can do things with it that you couldn't do otherwise.

ChrisW
+3  A: 

We mainly use languages for the abstractions they provide. In which case, no, there are many abstractions which are simply not available in C.

BlueRaja - Danny Pflughoeft
what do you mean by `abstractions`?
afriza
+3  A: 

Depending on what you're looking for, you may actually be able to do more in C than in C# or Java because you can get closer to the hardware level. The same goes double for assembly code: you can write very architecture specific code in assembly that is difficult or impossible to achieve in a high level language.

C and assembly are more specific and more permissive than C# or Java, but accomplishing things in them is more work. For instance, comparing string values in C# is as simple as using the == operator; in C and assembly you need to use a specific routine for that (if you have a stdlib implementation), or write your own (if you don't have stdlib).

So there are things you can do in C and assembly that you can't really do in C# or Java (like count exactly how many CPU clock cycles one of your functions will run for, or fit your code into an embedded device with only 4k of memory), and there are things you can do in C# and Java that you can't really do in C or assembly (like write a web service with only a few lines of code and in only a few minutes). It's up to you to decide which tool you need for the problem at hand.

Edit: I forgot to mention C++. You can think of it as a compromise between the two worlds, but personally I'd stay far away from it if you possibly can. It's ugly if not used with extreme care. Most problems are best served by either straight C or something friendlier like Python or C#.

Parappa
-1 for what is said about C++.
Billy ONeal
@Billy ONeal: Yes, that remark was clearly off base--when C++ *is* used with extreme care, it's ugly anyway. A very powerful and useful language, to be sure, but syntax only a mother could love.
camccann
@camccann: Yes. Syntax so ugly that it's been copied by most all other major programming languages in use, including C#, Java, and PHP. (Okay, you can have Python and Ruby...)
Billy ONeal
C#, Java, and PHP copy C's syntax, not C++'s, in my opinion. To be clear, I'm not some kind of wacky C++ hater; my caution regarding it comes from experience. It's a fine tool if used properly, but there are so many traps to fall into--it reminds me of Perl in that sense. Languages that have come since C++ have learned many lessons from what C++ did wrong, and in that respect it is foolish to use C++ where it is not truly needed. Think twice before punishing me for criticizing something openly and honestly.
Parappa
+1 for the "C can do more than C#/Java, depending".
Paul Nathan
+3  A: 

Yes, theoretically, you can, unless particular constraints preclude it (good luck writing a J2EE web application in C).

However, there is more to language selection than what a particular language makes possible. There is also what it makes easy and what it does for you. Take garbage collection, for instance. There exist garbage collectors (such as Boehm's) for C and C++, but Java and C# provide one with the run time and are built around having it available. There are cases where you don't want a garbage collector, but for most applications, good garbage collection means there is a whole class of problems the programmer no longer has to worry about. And that means more of their precious attention and time can be spent solving the real problem at hand.

Also, string handling and the host of things that depend on it (such as I/O). It is possible to do string handling safely in C. However, it is far easier in Java or C# (or C++ if you use an appropriate string class) and requires less redundant code (and more redundant code means there are more places to make an error).

So while you can do pretty much everything in C, there are good reasons not to, and students can benefit from being able to write useful programs without having to worry about the minutia of manual memory management. Certainly, they should do some C sometime in their education so they have a better understanding of what their Java or C# program does under the hood (and thus can better predict and understand its performance, etc.), but the ease of doing many things in higher-level languages gives them great benefit for both instruction and practice.

Michael E
A: 

Programming languages are simply tools we use to communicate with machines.

Like an artist would use tools (brushes and hammers and cameras) to create different pieces of art (paintings, sculptures, framed picture). It depends on the artist alot of times. Look at our classical works of art, there are many new brushes and sizes, and textures and all kinds of STUFF, but did leonardo di vinci need them to create masterpieces?

A good programmer can use the strength and weaknesses of what he has to work with to mold and shape his works of art.

There are platform specific tools just like a sculptor uses a hammer instead of a brush. But the point I am trying to make is, they are all tools. Its the art that is important.

RandyMorris
+1  A: 

Think of programming languages as human languages. If you write an essay about some topic in English, it can also be written in Spanish, French, German or Sanskrit. The essay is still going to convey the same central idea about which the essay was written. The same holds true for programming languages.

Its certainly advantageous to you if you can learn more than one [programming] language. Master at least a couple of them. Then you can easily read a code written in a different language and still be able to get the gist of it.

anand.arumug
+2  A: 

"Without a good library, most interesting tasks are hard to do in C++; but given a good library, almost any task can be made easy." -- Bjarne Stroustrup , quoted from POCO

I think for most languages, the libraries / frameworks available for that languages matter more to do any real works. And it happens that modern languages tend to have organized and cohesive libraries and frameworks which make them easier to use. It is more about how easy you do things rather than what those languages can do by themself.

afriza
I partially agree with this. A good example is PHP - in my opinion, the design of PHP is poor. However it has the best documentation I've seen for a programming language, and has an enourmous and excellent set of libs/built-ins.
Cam
+1  A: 

I'd guess universities and schools teach the most popular languages in the industry so that when you finish, you find yourself eligible for the most job ads out there. But if you find C more appealing to your way of working, you should learn it by yourself.

Any decent school or university should be interested in teaching the algorithms and data structures that go on to become the fundamental building blocks of any non trivial software. This they do it with a programming language. If you find your school giving more weight to the language than the ideas that underly the expressions(programs and softwares) you should get your self a good book to read on data structures and algorithms at the very least.

When I learned Object Oriented Analysis and Design, I was given a choice of C++, Java, C#, Python or any other object oriented programming language that was available on the university computers to program in. So that I can show my code compiled cleanly and worked as needed.

vpit3833
"... teach the most popular languages ... eligible for the most job ads ..."**The sound you hear is a teacher weeping.**
Norman Ramsey
@Norman Ramsey: Take comfort that you've the luxury to mourn, my friend; but for those of us who must live in that world, having glimpsed what else is possible, we have no tears left to be shed.
camccann
+16  A: 

Is it possible to write in C programming language everything that you could write in other languages such as Java, C# or C++.

Mostly. (Don't be distracted by Turing completeness; it's a red herring.) There are two sets of things that are hard to do in C:

  • Manage memory. In Java and C#, you just allocate objects, the system figures out when they're no longer needed, and the memory is recycled. In C and C++, you have to worry about managing memory yourself. (You have the option of Hans Boehm's libgc garbage collector, but many C programmers are reluctant to commit to it, and some people have reported difficulty getting it to work.)

  • Reuse code through inheritance and polymorphism in a type-safe manner. You can force reuse in C through judicious use of void * pointers and function pointers, but you lose the benefits of compile-time type checking. You just can't get the same guarantees as with C++ templates or Java generics. This is a compile-time guarantee, not a run-time guarantee, and Turing completeness is irrelevant to this argument.

Why don't schools these days teach C instead of Java?

(Some schools do continue to teach C, and almost all schools offer something in C.) For teaching, the main advantages of Java over C are that

  • You never have to worry about memory management. This is important because understanding heap-allocated data and when to call free is quite difficult for most students. (Hell, it's difficult for many graduates.)

  • It is much, much easier to write polymorphic code using generics; C lacks anything comparable to Java's type-checked generics. And reuse through polymorphism is a very important concept in programming.

Summary: It is true that all the languages in question are Turing-complete. But it is also true that Java provides compile-time guarantees that are impossible to provide in C, and it is true that while you can't write any more computable functions in Java than in any other language, in Java it is much easier to write code that does not have massive memory leaks. (Since Turing completeness assumes an infinite machine, it doesn't have to worry about memory leaks. But in the real world, use up a gig here and a gig there and you might run into trouble. My students do.)

Norman Ramsey
I disagree with the "don't be distracted by turing completeness"; on a fundamental level, any turing complete language can do anything any other turing complete language can, as long as both are compiled to machine code. The rest is just quibbling over features. Unless the question is about quibbling over features....
RCIX
@blwy10: Why "not including C"? C++ does not imply C in any means whatsoever. Just because C++ makes C backward compatibility a major feature does not mean that a good C programmer is a good C++ programmer or vice versa.
Billy ONeal
@billy: Actually, to be very honest. I don't remember why I wrote that... I'll edit it. It sounds kind of weird. Thanks for pointing it out!
blwy10
You don't have to manage memory in C++ if you use a smart pointer. And if you use RAII for all resources, memory or not, you don't have to manage any of them.
blwy10
Judging from the security holes found in many applications, memory management is too difficult for most *professionals*...
camccann
Memory management is not hard in C++ at all.
DeadMG
A: 

when you start you want to learn as many languages as you can. Later in your career that will pay off.

deian
A: 

No, at least not from any practical viewpoint. Consider a single example -- Java on web servers.

In theory, you could probably do the same things with CGI code written in C as is typically done on servers using Java. In fact, many ISPs wouldn't let you use them even if you could write them, and you'd have to (at the very least) re-compile them for use on different platforms. Worse, if you use Java you can easily find frameworks to handle lots of rather difficult tasks (e.g., cookies and persistence) at least reasonably well, but with C you'd almost certainly have to implement all this on your own.

At best, you'd do a lot of extra work for almost no benefit. At worst, they wouldn't work at all, because your ISP wouldn't let you even install them.

Jerry Coffin
+1  A: 

Hi Sahat,

I like the questions you ask as a student.

Is it possible to write in C programming language everything that

you could write in other languages such as Java, C# or C++. If that's the case why don't schools these days teach C instead of Java?

There are some good answers that get into the nuts and bolts of things here already. I'm going to answer this question with the topic of one of my first presentations at Uni. This was at a time when Meyer's book has just come out and OO was in it's infancy.

At the time I saw a remarkable similarity between what I considered "best practice" in the procedural world and what OO was doing for us. My premise was that whilst OO strongly encourages us to do some things in elegant and sensible ways, in procedural languages like C, we have the option to apply equal elegance if we just know how to use things in good ways.

In years to follow I realised just how important it is for the development environment to guide developers into doing things the right way. The reason for this is not everyone you meet has the same eye for doing things right as you, me and many of the other fine folks frequenting this forum do.

Of course the other thing is that whilst you can, practically speaking, accomplish pretty much any (commercial) result using any language/framework some offer significant productivity benefits over others. Just because you can do it, doesnt necessarily mean you will want to if you have to spend an extra month writing framework code that you can just call in another environment.

Ok the main reason why I am asking is because I don't want to tie down to

a single programming language and platform (.NET and C# or Obj-C and Cocoa). Perhaps I am confusing a programming language with a framework? If anyone could clarify all this for me, I'd certainly vote for your answer.

Again you've got lots of good info on the nuts and bolts of things here. I'm going to try to offer you some info that I think will address what's fundamentally the question in your mind here.

Do you need to learn lots of languages or just one really well to be relevant in the commercial world?

The same question can be applied to frameworks.

There are two answers to this for my money.

Firstly I'll tackle it from the angle of your skill development.

I believe you will learn immensely from going deep in a language and framework of your choice. Going deep typically comes form solving problems and putting all the tools of the language to work and sometimes pushing the boundaries. This experience I believe hones your capabilities to solve problems and recognise how to apply the same or similar techniques quickly in unfamiliar languages and frameworks you will encounter in the future.

With that said, yes its defintily an advantage to go broad as well - getting a taste of what the pros and cons are of different languages and frameworks and learning to evaluate which is better suited to different kinds of problem spaces. This is also an important part of your development.

You can drive whether you go for breadth or depth based on your interest and motivation on the day while on your own time. But I do recommend keeping an eye on both, even if just to break it up a bit and keep things fresh.

Secondly I'll tackle it from the angle of your career growth.

From my experience this really comes down to what you want to be doing. You've said in another post you are interested in financial work. This makes the answer simple since you know what you want to do. It's simply a question of where all the jobs are and also looking at where you see the industry going.

Making an assessment where there will be the best of their being lots of opportunities. More often than not it will also happen that there is a reason why a particular technology set is dominant (or emerging) in a particular industry.

From person to person you will get different views - sometimes loosely "religous" in motivation. I encourage looking beyond this at what is going to be best for you.

I think you already know which tools I spend my time with at the moment, but I always keep an eye on the other camps at what's going in. If there is one constant in this industry, it is that things change.. usually when you're not looking :o

Lastly, I will just say that OO has many advantages over procedural, there is no question of which is a better way to program for me. But don't forget functional - this style of programming is becoming more relevant now and is starting to make it's into the commercial world after a long stint mostly in academia. Check out some of Anders' recent discussions and presentations. You will get an idea how it's going to "fit in with" rather than "replace" imperative programming languages.

Mick N