views:

610

answers:

13

This is not meant to be inflammatory or anything like that, but I am in the midst of learning C, and (think) I have a good handle on most of the basics. I've done all of the various book exercises: primes generators, Fibonacci generators, string manipulation, yadda yadda, but none of this is cool.

What is the "bridge" between command line programs and something -cool-? Is there a GUI library for C? I've heard of various games being written in C, but how? Is there a good resource of libraries somewhere?

Forgive my exasperation, but it feels like I've been learning lots but can still only do relatively little. Thanks for any insight on what to do with C.

Relevant information: OS X leopard, PHP and web development experience (which is so great because projects are immediately in a context where you recognize how they can be powerful)

+3  A: 

Cool stuff do with C?
Operating Systems, device drivers, and python modules for starters.

Games typically will use C++ if they're C-Based, in my experience / what I've seen.

Aaron
Games "typically" use C++? Do you have a source for that? Most of the game code I've seen is actually C mixed with assembly language.
Daniel Pryden
With the games / engines I've personally been interested in, they have been Cpp. Examples being the Unreal Engine and CryEngine. The Unreal Engine is used in many, many modern games and is licensed to many 3rd parties.
Aaron
+3  A: 

There are many libraries for C under Unix, such as X lib, which accesses X11.

If you wanted to get into robotics you may find C to be very useful, as you will have to write low-level code with very small memory footprints, so even C++ may not be the best choice.

C and C++ are very good at writing small, fast code, but OOP is not always the best choice, so at times you will find that C is a better choice, for example if you are writing a compiler or OS.

James Black
+6  A: 

Every major operating system has all of its low-level libraries implemented in C. Mac OS X is a Unix-like system under the hood, which is a wonderful world if you're a C programmer.

Check out The Art of Unix Programming for some great ideas.

As for GUI stuff, you'll probably want to use X11. There is plenty of good documentation out there -- most Unix programming stuff and most deep system-level stuff just assumes you're working in C, since that's what everybody uses for it.

Daniel Pryden
Don't forget Apache. Written in C. Most RDBMS's. Written in C.
S.Lott
+4  A: 

Well, that depends. If you want to build desktop applications, a multiplatform GUI library whose main language is C is GTK+:

http://www.gtk.org/

For games, check out SDL:

http://www.libsdl.org/

Which provides you with thinks like direct input from keyboard, 2D graphics, some audio and even threads and stuff like that. It can also open an OpenGL context if you want to get into the 3D world (however it's hard to do it in raw OpenGl). Did I mention that SDL is multiplatform?

However the real strength of C is in systems programming. For desktop applications/games maybe you are best suited with C++. Now that you have command of C, learning the basic C++ should be easy ;).

machielo
+1 but I wouldn't discourage game programming in C. It can be done, and whether or not it's any better in C++ than in C is arguable. It may be more commonly done in C++, but that doesn't necessarily mean it's better.
Chris Lutz
"Learning the basic C++ should be easy" - from a language point of view, yes - but using objects properly is a completely different kettle of fish.
Smashery
You are right. Doing a 2D game in SDL is fun and you can learn a lot from it. Also doing some simple stuff in OpenGL. For more complicated things, well, it requires a lot of knowledge and perseverance, but of course it can be done.
machielo
True, by "basic C++" I meant the C subset of C++. From that point on, it's time and work, as usual :).
machielo
+1  A: 

SDL is a good library for graphics and sound, and I've seen some cool stuff done with it. If you do it in C, it'll take longer to make, but from a performance point of view, it'll be much better.

Smashery
But if you're not worried about performance, aren't worried about people judging you for using a "lesser" language, and just want to make cool stuff, I would recommend trying a different language. I've worked a lot in Python using the Pygame library (which is actually just a wrapper on SDL); it's very easy to use and get used to, and you can make some very cool stuff with it. It's not going to be as fast as pure C, but if that's not a concern, my opinion is: why bother?
Smashery
+33  A: 

C is the concrete and the steel of modern tech

There was a time when almost everything was written in C, or in something much worse.

These days, many of the advanced languages and systems are actually implemented in C or C++, and then those things implement more systems. It is standing on the shoulders of giants, as the expression goes. Almost every OS kernel, browser, and heavy-duty-web-server is written in C/C++.

So sure, you don't see the steel in the high rise, you see the beautiful interior furnishings and the sleek glass windows. You don't want a steel or concrete desk, and if you did, it would be too expensive to build for you.

Back to your GUI question: your first C graphics program should probably use the original X Window System directly. Don't spend too much time there, but then move on to one of the more advanced Widget toolkits such as GTK+ or (the C++) Qt. Be sure to investigate your OS X system, as it has one of the most advanced of them all.

I try to write things in Ruby these days, but I happen to know there are over 100,000 200,000 lines of C code implementing that cool Ruby language system. :-)

DigitalRoss
Good advice, except that Qt is C++.
Josh Kelley
+1 on OS X, Cocoa is written in Objective-C which is extension of C so it helps to know C, plus Carbon (lower levels) is all C ...
stefanB
You can also of course use the Win32 api, or openGL, or directX all in c, without any c++ constructs, not just X Windows.
Darien Ford
There are some GUI library for C (not as many as for C++). The one I would suggest for completeness, portability and ease of use is IUP (http://en.wikipedia.org/wiki/IUP_%28software%29)
Remo.D
Ah, C. You are the reason I love programming. +1 For recommending going with X before GTK+ and Qt, there is a lot of presupposed "magic" as soon as you get into widget libs.
Matt Joiner
+2  A: 

Sure there are some impressive programs made in C !

GNOME for example, arguably the most used desktop environment used in modern unix systems is written in C (the major parts at least) and is mostly based on GTK+ gui toolkit, itself done in C.

For game, OpenGL is a C api and is the standard for 3D graphic programming in multi-platform development (not uniquely microsoft platform), and Quake 3, which the engine, Id Tech 3, is available in GPL, is also writen in C. There also is many 2D games written using SDL library.

Aszarsha
+1 for OpenGL. If that's not cool, I don't know what is.
Carl Norum
A: 

If your idea of cool is GUI apps and you want to write native GUI apps on the Mac, you'll want to look at Carbon. This is the official C API into the Mac GUI and OS. They keep threatening to kill it, but it survives.

Personally I think GUI apps are a very narrow definition of cool. What I think is cool is implementing parallelized math algorithms using opencl.

Charles E. Grant
+10  A: 

Summary

Ok, this post got really big, so here's a quick summary before you read it: to program GUIs, there are a number of good C/C++ libraries (for example, QT). What most people do, on Windows systems at least, is to use a .NET language (like C#) when they want GUIs, and C/C++ when they want more control/speed. It's also very common to use both in combination, i.e. make a GUI in C# and speed-critical parts in C.

I still encourage you to read the longer answer, it contains a lot more information on your options.

Longer answer

I'll start with the big question, then answer (as best I can) your specific question about creating a GUI. I think you're kind of suffering from the fact that C is used to teach programming, and it's much easier to do so only using command line programs (after all, they're much simpler to write). This doesn't mean that C can't do all of the stuff you want, like GUIs specifically. It does. I don't think there's any type of software that hasn't been done in C, usually before it was done in other languages.

All right, some answers:

Is C Useful?

C, and its very close relative C++, are responsible for a huge portion of the world's code. I don't know if more code is written in C than any other language, but I'm guessing it's not far off.

Most of the really important programs you use are actually written in C/C++. Just for one example, Windows.

Where is C used today?

C/C++ are still used a lot. They're especially useful for developing low-level stuff (i.e. stuff like Operating Systems, which need a lot of speed, a lot of ability to control exactly what your code does, etc.).

But don't think it's all low level for C programmers. Even today, with many other languages available (which are arguably much better, and certainly much easier to program), C is still used to create practically everything. GUI applications, which you specifically asked about, are very often made with C, even though nowadays, lots of people are switching to other languages. Note I say switching: C used to be the standard language for writing, well, everything, really.

How do I develop GUIs with C

Alright, you specifically wanted to know how to create a GUI with C (I'm hoping C++ is ok too).

First of all, it depends on a number of factors:

  1. What Operating System are you writing for? (Windows, Mac, and Linux are the most common).
  2. Do you want the GUI to work on other systems as well?

The most common case is writing software to work on Windows. In that case, the "natural" solution is to write things that work with the Win32 API. That's basically the library that "comes" with Windows, letting you do any GUI work you want to do.

The big problem with this is, it's kinda hard. As in, a lot hard. This is the reason most people don't do that kind of work anymore.

So what are your other options?

The most natural is going with what's called a .NET language. Those are a bunch of languages, together with libraries, that Microsoft created. They're probably the easiest way to get a GUI on Windows. The problem is, you can't really use them from C (since it's not a .NET language).

Assuming you want to stay in C/C++ land, you can use some kind of library which makes working with the Windows API easier (since it hides all the ugly details):

  1. One of the most common is what's called MFC (Microsoft Foundation Classes), which are a bunch of C++ classes which make it "easier" to create Windows stuff. Unfortunately, this library is very old, and is really not that easy to use.
  2. The other way to go if you want to program in C++ is to use some kind of third-party library. This is a library that someone other than Microsoft created, which makes it easier to create a GUI.
  3. Another option is to create only the GUI part of your software in a .NET language, and use C/C++ for the other parts (or use the .NET language to do almost everything, and use C/C++ only when you really need it, for example when you need things to go really fast). This is a very popular option.

The advantage of a third party library is that, if you pick the right one, you can use the same code to create a GUI for all the Operating Systems at the same time.

I think the most famous of these libraries is QT, and also WxWidgets, but there are a bunch of other ones. I would personally pick QT since it seems to get the most fame, but I haven't worked with either.

Edan Maor
OK, kinda didn't notice that you said you're on OS X. Oh well, hope this answer is still informative for someone sometime.
Edan Maor
+1  A: 

GTK-server is REALLY easy to get started with, in C or any other language. Just click that link.

Amigable Clark Kant
A: 

Last I checked more Open Source projects are started in C than in any other language.

Philip Schlump
A: 

For a "cool" application that goes beyond simple GUI's, check out the OpenCV computer vision library. It provides fast real-time image processing and face recognition.

Now you can access a webcam and start writing real-time computer vision games. For applications like these that are processor intensive, C is the ideal choice.

AndyL
A: 

The fact that C is used by so many large and successful projects doesn't particularly make it "good". The reason C is so commonly used is because of a few factors, it's been around a long time, it's fast, it lets you access both low level and high level interfaces as needed, and it's better than the other old languages (FORTRAN etc). The "cool" thing about C is that you can make it do absolutely anything: inject itself into the kernel and add some new features or bug fixes that you couldn't convince Microsoft to do, etc.

Yes, C can be and is easily used for things beyond the command line, but it's extremely dangerous due to pointers... Not to mention development in other more modern languages is faster (and safer) by magnitudes. I never use C unless it's the last resort, ie: need to implement something low level or needs that extra performance.

By the way, when I say C, I really mean C++. I'd never choose C over C++ unless I was forced to.

Longpoke