Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm currently learning C via 'C Primer Plus' as recommended by a lot of people here and I'm finding it very interesting - the in-depth explanations of how and why we do certain things in C are great rather than just being told what things do and going no further...

I feel as though all I can do with C at the moment is maths, which isn't terribly inspiring when trying to picture putting various applications together in my head (besides calculators (-_-)). I'm only about half way through the book so it hasn't covered things like the if statement yet, but I'm just taking it easy - I already know a fair amount of programming concepts since I'm fairly capable in PHP.

My question is - are there any tutorials are interesting bits of source code people can link me to that will inspire and educate me beyond the loops/variables/operators/etc universal programming features that will give me an insight into programming say.. with Windows API? Or things along those lines?

I found PHP fine to learn since I already knew html/css which obviously handle the front-end of my web applications, as well as the reverse - getting input data from forms, interacting with a database etc, but with C I don't know anything other than 'backend' and standard I/O stuff.

Many thanks to those that can link some interesting resources - a lot of sites seem to be fairly old, which is fair enough (what, with C having been around for coming up 40 years?), but I'm not programming for 40 year old computers so...

share|improve this question

migrated from stackoverflow.com Jun 9 '11 at 21:34

This question came from our site for professional and enthusiast programmers.

    
I'd suggest you to stay clear of the Windows API, which is truly horrible to work with, and to try something cleaner, like GTK. –  Vitor Jun 9 '11 at 22:55
    
What is your motivation for learning C, exactly? –  zvrba Jun 10 '11 at 8:18

8 Answers 8

You must first learn all the basic math to understand the basics of code, later you will move on to if statements, classes, and from there you can make an application with controls and buttons that can do everything you'd like it to do, just keep on reading, the basics are boring but it gets more complex and as it gets more complex you get more possibilities.

share|improve this answer
1  
Thanks, I have every intention with sticking with it. :) Would just be nice to see a glimpse of light at the end of the tunnel, if you know what I mean. :P –  Anonymous Jun 9 '11 at 21:27
1  
Not necessarily, programming != math. I would say good grasp on data structures, pointer arithmetic and programming style is more valuable than math. I guess around 75% of CS problems are not related with math at all. –  Coder Jun 9 '11 at 22:02
    
@coder: I find it curious that you wouldn't consider data structures and pointer arithmetic related to "math", but maybe you have a much narrower definition of "math" than what computer scientists and mathematicians consider "math." –  JasonTrue Jun 9 '11 at 23:31
    
Well, for the data structures you usually use STL or some other alternative, and not rewrite them from scratch, so major math stuff is already skipped - nlog(n) etc. And pointer arithmetic rarely has polynomials in it. Most pointer math I've seen so far was pretty simple, takes a lot of caution, but it's simple from mathematical standpoint. –  Coder Jun 9 '11 at 23:37

Back in the day, Charles Petzold's book was the way people learned to program directly to the Win32 API using C. It's old, but the Amazon reviews indicate it's still completely relevant.

Having programmed GUIs for Windows for about 20 years, I would suggest one thing. At the heart of every Windows program is what's known as a message pump. Many programming systems hide this from you, but if you write straight C, you have to do that yourself. Anyway, part of the pump is a message interpreter, and Petzold writes his as giant switch statements. That's okay pedagogically, but a lot of people took that up as a coding style, which makes for code that's very hard to maintain. So instead of loading all your message processing code into the switch statement itself, the way Petzold's examples do, have each case call a routine that processes that message type. (There are other ways to do that which I prefer, but they're a bit fancy if you're just learning C.)

share|improve this answer

I'm not sure I understand your problem. C is a systems programming language, intended for solving problems like these:

  • how can I copy this file from one machine to another
  • I want to see if these directories have changed
  • I want to write a program that will serve files on the web

These all seem interesting, non-mathematical problems to me. The second C program I wrote (25 or more years ago) was to write APL program source to a dot matrix printer. Also non-mathematical.

Perhaps your problem is that you have not yet come up with an interesting problem that C can solve for you? But that is deeply, deeply your problem - no-one else can come up with a solution for it.

share|improve this answer
    
It sounds like you exactly understood my problem - I want links/source codes to programs in C that solve non-mathematical problems. :p –  Anonymous Jun 9 '11 at 22:17
2  
@Chris Um, like the source code for the Linux kernel? There are a billion such resources just a google away. –  nbt Jun 9 '11 at 22:29
    
Probably a "bit" complicated at this stage, all I'm after is a few good, simple, well-written/structured programs to educate myself from that are more than a calculator/convertor. –  Anonymous Jun 10 '11 at 5:51
    
saying "a billion resources exist over there" is perhaps a bit vague. Could you perhaps link to a few of the good ones? –  user1249 Jun 11 '11 at 20:00
    
Isn't easier to do that things in a Scripting Language like Perl or Python ? –  dysoco Sep 5 '11 at 15:32

Since you're learning C, get a good understanding of pointers and all the nightmare that comes with them. Learn to think what bits do.

Then you can pick some interesting topic like programming a game engine with OpenGL, or a GUI app with WinAPI. They are both C oriented.

share|improve this answer
    
Link by the way - nehe.gamedev.net –  Coder Jun 9 '11 at 22:29
1  
There is no more a "nightmare" that comes with pointer than there is with integers. –  nbt Jun 9 '11 at 22:31
    
It's C! It's not RAII and Java. Of by one errors during dereferences, 32 bit to 64 bit migrations, alignment, leaks on unchecked exit paths. Almost every C app has a memory leak or a buffer overrun problem. That's the reason we have those strcpy_s functions and SDL and stack cookies, and even that doesn't help much. One incorrect place where you dereference something and your app is a toast. –  Coder Jun 9 '11 at 22:46
1  
@Coder Sorry you can't handle it. But lose the bullshit. I am the last person to recommend writing code in C, but this is nonsense. –  nbt Jun 9 '11 at 22:55
    
How do you explain security and buffer overflow fixes for every major software product out there? Incorrect integer math or overflow? It's way more less common than pointer bugs. –  Coder Jun 9 '11 at 22:58

This may not fit what you want to do, but you could consider using a higher-level language than C. You would be able to do interesting things more quickly this way (unless the things that interest you are inherently fairly low-level operations).

share|improve this answer
    
I'm not expecting to be able to do things faster, I appreciate that C isn't a terribly high level language (I always have trouble sayings its a "low" level language since... is it really? machine/assembler/binary? :P). I just wanted a few good examples of c programs that do more than 5 + 5. –  Anonymous Jun 10 '11 at 5:50

Um, dont let people scare you away from Win32 API programming. It has alot of different structs & functions & classes & interfaces but it also has ALOT of documentation & openess about how the operating system works (dont expect that from Apple & MacOS or iOS ;) )

Once you start making pure win32 app's you will discover how an OS works, be able to make Windows apps that work on any version of windows & discover how much more powerful you are at making the computer do something(& do it fast) than a fiend who uses wxPython or c++ QT.

My first win32 apps were:
- Paint CLone: draws objects onto the screen with the mouse
- Random playlist generator: allow you to find, open, delete files of different types
- Task Scheduler Simple Library: easily schedule tasks on any version of windows (very handy)
- Created my own screen saver
- Input a function for a graph (y = mx + b) & draw that graph using GDI

share|improve this answer
    
That's the type of stuff I'm interested in. :) –  Anonymous Jun 10 '11 at 5:52

If you're not interested in the windows API specifically but just want to learn how to write more complex programs in C then set up a Linux VM and install source packages from whatever programs you find interesting. C is the lingua-franca of the *nix world and there are a ton of applications ranging from absolutely trivial to the nightmarishly complex. Plus, if you are interested in GUI programming in C you'll have access to GTK+, which in my (not exactly popular) opinion is a nicer GUI api for C.

Even if you decide to stick with Windows, if you're using Visual Studio I'd suggest downloading GCC. Visual Studio's C99 support is practically non-existent and there are a lot of nice features in C99 that make using C much more enjoyable IMHO.

share|improve this answer
    
I've been using whatever C99 standards the book has been suggesting, just assuming than a compiler 12 years down the line would support it lol, I'll take a look at GCC. –  Anonymous Jun 10 '11 at 5:55

I think the first 'interesting' thing you're going to learn with a C primer book is how to manipulate files - and really, that's a good place to begin making more complex programs. Until that point, everything is essentially a toy program.

I find the easiest way to deal with the Windows GUI is using SDL. It gives you a nice set of tools to work with, while still giving you complete control over the contents of the window.

If you want to mess with 3D, on the other hand, NeHe's OpenGL tutorials are a great way to dive right into that - he gives source code examples that create a minimal framework that's just enough to provide you an OpenGL rendering context.

He also does his best to explain what each line is in the Windows setup; it'll probably be over your head at first, but once you start to understand how the message pump works, it'll make more sense.

Of course, even that is only of so much use until you learn how to open and manipulate files. :)

NeHe's tutorials can be found here:

http://nehe.gamedev.net/

share|improve this answer
    
Thanks Mike, I'll checkout the gamedev tutorials in a few weeks probably rather than rushing into it. I'll stick with the book until I've learnt a fair bit more. :p –  Anonymous Jun 10 '11 at 5:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.