PDA

View Full Version : How to learn C from a C++ background?



AtmaWeapon
01-09-2005, 03:23 PM
How's this for a topic with a limited audience? I've been picking up game programming books over the past couple of weeks, and they all work with C instead of C++. Now, I'm not nearly as experienced with C++ as I should be, but I know enough to read anothers' code and understand what's going on in most cases. I'm not having any real problems reading the code and figuring functions like printf() out, but I know that C is structured while C++ is Object-Oriented.

I've been doing Object-Oriented work for the past 4 years, and really the only structured programming I've done was stupid projects in Turbo Pascal. That was way back in the "OMZ I'M PROGRAMMING" phase and I didn't really understand what I was doing.. Any serious work I've done has been in VB .NET. It took all of those 4 years for me to fully understand just what programming in an Object-Oriented style means, and here I am wanting to work with a structured language again :D. As I said, what's going to trip me up is not so much a syntax switch but a paradigm switch.

So what's a recommended book(s) for an intermediate programmer to get the most out of C? I'm sure there's plenty of valuable web sites out there, but I really dislike reading books online. There's something I like about a book as compared to an online source, I think I just like to see my bookshelf overflow more and more :).

vegeta1215
01-09-2005, 07:50 PM
I know how you feel. I prefer reading books in hand as opposed to online also. I'm sorry to say that I can't recommend a book though, cause I started with C++ like you did, and thats what most of my books are about.

This is a pretty handy C reference site though. (reference) http://www-ccs.ucsd.edu/c/

Lutraphobiac
01-09-2005, 09:38 PM
I have the C for dummies books, but I wouldn't reccomend them. My engineering textbook has a C section, but I can't give you that.

...really no point for me posting this. As Vegeta said, most books now are on C++. Maybe you could check for textbook stores for a C book.

Kryten
01-10-2005, 01:56 AM
It's not really that bad, you mostly have to get used to not being able to do certain things and learning how to do them another way (the C way). It's much easier than moving to C++ from C because you have to get your head around OOP (difficult for many) and there are a lot of things you're not allowed to do anymore (mainly involving VOID pointers).

Really, the only parts of the language you might not be familiar with are structs and having to do your own type conversions. It's the ANSI standard libraries that you need to brush up on, and those are all documented in good detail across the web and in most compilers.
Once you figure out what you need to do to print stuff (printf/scanf), convert between formats (atoi, sprintf/sscanf), and work with files (fopen, fprintf/fscanf, fseek), you're pretty much good to go.

If you want a good book on C, head to a used bookstore/site and pick up one of the "classic" C books. By "classic" I mean "the ones everybody agrees is really good". Seeing as I learned C mostly by myself, I wouldn't know what those titles are :p.

Oh, and you can always look into "3D Game Programming with C++ Gold Book" if you want to look at some game programming with teh ++. Co-authored by Andre LaMothe (rather than just "edited by" like his more recent works), so you know it's good.

Ganonator
01-10-2005, 02:08 AM
i originally learned C++ before venturing into C, and it was very rocky at first until I learned what I couldn't do in C that I was used to in C++.

I would suggest not buying a book, but doing small projects in C, and working up to something of meaning.

if you would like, I can host digipen's C website for public use.

Sorry that i didn't give you any meaningful advice, just look online for resources. If you are programming games, check out gametutorials.com.

Cloral
01-10-2005, 02:39 AM
Just remember that C is a subset of C++. If you know C++, you already know C. The main difference is the absence of objects. So where you might do:

class blah {}

You do

struct blah {}

And then instead of having member functions, you have to use global functions. That is the main problem with C - everything is global, so it is easy for some code to have unintended side-effects. This creates hard to track down bugs, so you have to be careful.

...
You know, it might actually be a better idea to take what you see in C, and convert it to C++. If nothing else, you can just take a chunk of their code, throw it in an object, and then just use that object elsewhere (i.e. if they have code to do graphics or networking or that sort of thing). C++ is used in the industry - I know, because I use it everyday. In the end it is cheaper, because while it might be a bit less efficient when run, it can be written much faster and is less prone to bugs.
It is likely the books you are reading were written by people who have been in the industry a while - that would only make sense. So when they were getting into the industry, C was likely the predominant language. So they are probably still more confortable with it than C++, so they decided to write the book in C.

So to answer your question, I wouldn't try to adopt a C-style of programming. That would be taking a step backwards. Just try to understand what their programs are doing and then do it better in C++.

PS: What sort of programming experience do you have? Have you taken classes? If not, I would suggest you seriously consider doing so if you are interested in this stuff. There is far more to programming than just knowing the language, and unfortunately it is hard to learn those sorts of things (OS design, multithreading, data storage, network synchronization, time complexity, etc) from books and online tutorials.

AtmaWeapon
01-10-2005, 09:52 PM
So far the little bit of code I've encountered pretty much shows me what I need to know about the structured approach is that good structured code approaches some quasi-OOP state. For example, suppose there is a ball struct, and the program keeps up with several of them. The UpdatePosition function that in OOP I would place within a class is now placed in global space, and takes arguments to indicate the ball it will work with. No big deal there, I thought maybe there was more to it.

What little research I've done reveals this tome (http://www.amazon.com/exec/obidos/tg/detail/-/0131103628/qid=1105405279/sr=8-1/ref=pd_csp_1/002-1018452-6514408?v=glance&s=books&n=507846) to be some sort of sacred text for coders, impressively receiving more reference than The Llama. I think I'll look into it. Whether C++ or C is better or not doesn't really matter; I feel like if I am going to put C/C++ on my resume I may as well learn to use C. Plus I think owning that book would be a good idea because it probably provides a CHA bonus and can cause random status ailments to programmers with a class level that is 5 levels lower than mine.

Since it was asked, I'll mention my programming experience. In one of my coworker's Perl books, Larry Wall claims that the three traits needed for effective programmers are laziness, impatience, and hubris. I will not pass up this chance to display hubris since I'm certain at least four of you can smash my ego back down :p.

I started something like 8 years ago in Turbo Pascal 7.0 and managed to write a Tic-Tac-Toe game that I was fairly proud of (I spent days playing games against myself developing and testing the AI :)). From there I moved to self-taught C++ but never really got anything done. About 3 years ago I hit college and learned some more C++, but still didn't really produce anything of note. At one point I tried to use C# but because I was stupid then and too lazy to read documentation I wrote it off as "too hard" and "not worth the trouble". Last year I entered a co-op position at a manufacturing plant and I was told I needed to learn VB .NET. In 2 weeks' time I was productive, and in 3 months' time my skill in .NET eclipsed my C++ abilities. The product of the first term was a MS SQL Server database application that featured both simple and complex databinding (biggest misnomer ever; simple databinding requires 10-15 lines of code for every line that complex databinding requires) and featured update conflict resolution that allowed the user to view the original entry, their change, and the database's current value and choose between the three. In addition, I experienced scope creep firsthand and grew a healty fear. The last 3 months were spent on odd utility projects. One is a program that pulls information from the Event Logs of several remote computers and displays the data through a ListView with a flexible filtering engine. The other is a program that scans folders on several remote computers for files that match certain extensions and generates reports on the files it found. Both use multithreading, though the second is much more refined because the first project was where I cut my teeth.

I've dabbled in Perl and PHP but never really gotten anywhere with them. When I moved the Member Pages to my domain I used a Perl script to fix all the links. I really don't know how a language can be so ugly but do such beautiful work. I know some rudimentary Win32 API programming with C, but not enough to really get anywhere.

I have way too many personal projects going, but here's what's going on and the status of each project, who knows, maybe one of you can help me with the things I'm stuck on.


1. Program for fiancee: Eventually will run as a screensaver and display various statistics and messages about our long relationship. Currently halted because I've never been good with date calculations and the .NET TimeSpan class only lists up to total days. Anyone know some good "Years Months Days Hours Minutes Seconds between two dates" algorithms? I'm also struggling with the piss-poor text measurement facilities in GDI+.

2. ThoughtList: theplustwo thinks I forgot about this one. It's a task manager program that will move in three phases from a TreeView-oriented design to a custom control that merges TreeView and ListView to a virtual whiteboard that you place "Post-It" type notes on and draw lines to indicate relationships. Currently halted due to lack of free time and I'm really at a loss on how to combine the TreeView and ListView... I've seen some C++ examples but I want it done in .NET and I want to write the code myself, not to mention they were MFC and deciphering Perl is much easier than MFC.

3. Express Yourself: Regular Expression testing tool that is a blatant ripoff of Chris Sells's Regular Expression tool. I started the project because his tool was once open-source freeware but like several other prominent .NET open-source projects he decided to remove the source and make the program shareware. Currently reeling due to my hubris. The original displays the source strings with matches and replacements highlighted and hovering over the region displays a ToolTip with information about the groups within the match. He accomplishes this by generating HTML files and displaying them in the MS HTML control. I felt this was silly and decided to accomplish the same with the RichTextBox control. Everything is perfect except for one bug that I will detail in the vain hopes that someone has a solution. RichTextBox.GetCharIndexFromPosition will return the character closest to the cursor, which means in areas with no text a ToolTip can still be displayed. This should be as simple as determining the boundaries of each visible line and testing if the mouse is outside these boundaries. Horizontally this is no big deal, but vertically there is a distinct challenge. Font.GetHeight is supposed to return the distance between the baselines of two lines of text, which sounds like a reasonable estimate of the line height to me. Given the height of the font, it's a simple matter to calculate the position of each line, right? Wrong. (http://www.atmaweapon.org/images/EE/oddity.png) Due to some undocumented distance that I don't understand, GetHeight is only accurate for specific font sizes. So far my only workaround comes in the form of RichTextBox.GetPositionFromCharIndex(). For line n, the position of n represents the top left of the rectangle and the position of n + 1 represents the bottom left. This still poses a problem for when n is the last line of text, but I think in this case GetHeight may be sufficient. This has been upsetting me for months now.

4. AGN Member Pages: Sometime in the last few versions of FireFox the Member Pages stopped displaying properly. They need an update anyway, so I figure I'll give them an overhaul.

5. Pong Screensaver: This is a screensaver that displays a Pong game between two computer players. I have the ball logic down, now it's a matter of overcoming laziness to implement the paddles and paddle AI.

All projects but the Member Pages are in VB .NET, though I'm thinking of doing the screensaver in C# for a change of pace. The book I'm reading now covers Allegro, but it's going to be a while before I submit myself to the task of writing a game. I think a Minesweeper clone is the first thing I want to write.

Ganonator
01-10-2005, 09:57 PM
About the Ritchee text:

I had to use that book as a textbook my 1st year at digipen. It is not a textbook, but more like an operation manual for the language. I found that most programmers with any background frowned upon the book.