Learning C/C++ code

Recommended Videos

Agayek

Ravenous Gormandizer
Oct 23, 2008
5,178
0
0
cainx10a said:
Also, consider C# after you mastered the basics, it's the future of programming or so I have been told.
Needs more functional languages IMO. Haskell 4 lyfe.

Reep said:
Would Visual Basic be a good starter language? I saw it was advised to at least know some language before attempting C/C++.
VB isn't bad as a learning language, but actually coding in it is a nightmare. There's a lot of extra bullshit that VB requires that gets old, and there's some functionality it's missing that also gets annoying pretty quick.

I learned on Java, which is nice and user friendly and relatively easy to get into, and there's no way to have pointer errors or mallocs go bad.

@OP, there's one very important thing you should know before you do any work with C:

Have something easily repairable on hand. Seg faults are the fucking devil and I guarantee you will want to smash something due to one (or more) at some point.


Other than that, what Kaelan said. Learning the basics now will definitely help in the long run, take it from someone who's been through that. It will frustrate you to all hell and back now, but it will make things much, much easier down the line.
 

Agayek

Ravenous Gormandizer
Oct 23, 2008
5,178
0
0
the_abhorsen said:
I still see the words "syntax error" in my nightmares...a very unhappy two months...
Syntax errors at least tell you where the error is.

It's a lot more fun trying to figure out precisely where in 1500 lines of code, that someone else wrote, it goes FUBAR.
 

Ushario

New member
Mar 6, 2009
552
0
0
Oolinthu said:
Thanks for linking that site Oolinthu, I decided to give it a look over and it took me less than a minute to learn something new. From my brief look so far I would recommend it.

Who knew I'd been using an incorrect form of hungarian notation!

I started with C++ and while it has been a real trial at times I'd like to think it has paid off.
 

Nutcase

New member
Dec 3, 2008
1,177
0
0
Cixelsid said:
halflobster said:
I'm 16 and broke but I want to learn how to code in C/C++ does anyone know any websites that might help me with this.
I currently use Dev-C++ as my compiler. I know some basics but i cant find any in-depth information on it.
Are you learning from scratch or do you have some programming background? If you've got no programming background then I'd suggest you start out with something easier, like Pascal, Delphi, C# or Java. But first learn the basics of imperative languages before moving on to the object oriented languages (C#, Java, C++, Smalltalk et al).
If you want to learn a purely imperative language, then I'd say C is the one, since very few other such languages are in use today. I don't think you need to start with C - it's hard to get something useful done with it at first as it involves so much manual labor - but if you are motivated it's a good first language and you really learn from the ground up.

IMO, Python is a fine choice for first language - simple language, simple syntax, lets you learn what programming is about without worrying about details like memory management, and lets you easily make stuff useful in the real world which is motivating.
C is not typesafe and it does little to no checks on pointer boundaries, meaning you can write to your stack and heap space willy nilly. C++ is a bit better but you need to follow the rules, a lot of its unwritten and at times they are confusing unless you understand how the compiler works and how its memory model works.

Bjarne Stroustrup, creator of C++ said this:
"C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg."

That is exactly it, C is so powerful because it doesn't ask any questions and doesn't hold your hand. You ask "I want to shoot off my bigtoe." It will answer, "Sure, no problem".

I've been writing C++ for 10 years or so and it still manages to spring a surprise on me every now and again.
Ergo: with C, you might blow your toe off many times before learning not to do it. With C++, you never stop blowing your leg off.
I can't end a discussion on C++ without mentioning Scott Meyer's books: "Effective C++", "More Effective C++" and "Effective STL"; anyone who thinks they know a bit about C++ should read those books, they are very useful resources and provide excellent C++ tips and tricks.
I have read the first two, and they are a very good exposition of why anyone who doesn't specifically need C++ should stay the hell away from it. Especially as first language.
 

Nutcase

New member
Dec 3, 2008
1,177
0
0
Also, when it comes to learning an entire programming language, there is no way you are so broke you can't afford one book. (Or so lazy you can't pick it up from the library.) If you decide C is what you'll learn, then get a C book. As far as I know, there is no website on C that equals a good book. Learning a language is not a matter of knowing a few nifty tricks.

"Programming in C, 3rd edition" by Kochan is a sure bet. It's one of the few C books updated to the latest C99 standard, and wordy enough for a new programmer.

"C Programming Language, 2nd edition" by Kernighan and Ritchie, the original designers of the language, is good but too terse considering that you are new to programming and going to be learning alone.
 

rabidmidget

New member
Apr 18, 2008
2,117
0
0
accelerated c++ is a good book but it expects you to have already have experience in programming in general, it teaches you c++ not c with some other stuff
 

Kakkoii

New member
Jan 29, 2009
21
0
0
Don't use Dev C++, pretty much any professional developer will advise you to use Visual C++.
http://www.microsoft.com/express/

If you really don't want to use it, then the best alternative would be Code::Blocks.
http://www.codeblocks.org/
 

Nutcase

New member
Dec 3, 2008
1,177
0
0
Kakkoii said:
Don't use Dev C++, pretty much any professional developer will advise you to use Visual C++.
http://www.microsoft.com/express/

If you really don't want to use it, then the best alternative would be Code::Blocks.
http://www.codeblocks.org/
Why would you recommend someone new to programming use an IDE at all?
 

Cixelsid

New member
Jun 25, 2009
42
0
0
Kaelan said:
3. Forget about "easier languages". I'd recommend going with C then C++ instead of whatever else (C#, Java, etc). You can pickup those in a weekend once you properly understand C++ - it's better to struggle with the low level details now, while you're still learning, than not learning it at all and later on having to correct a swath of misconceptions and bad programming practices/habits you may have built up because all the little details were taken care of for you behind the scenes.
Kaelan, I understand your reasoning and to some extent I agree with you. A lot of lazy programmers should learn a bit of C++ in order for them to be conservative with their memory management. A language with a garbage collector is a luxury that breeds inefficient code. That is, until you understand how the garbage collector works and you start specifically coding so that it can function correctly.

But I don't think a person who's never coded in his life should just jump into C/C++, and I don't believe any person can say they "understand C++" until they've been coding it for a few years. I've met a lot of junior programmers who say they "understand C++" and then when I ask them to write an overloaded assignment operator for a specific class they never get it right. Hell, even the experts have a hard time agreeing, e.g. Anatomy of the Assignment Operator [http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html] and it's revised companion Assignment Operator Revisited [http://icu-project.org/docs/papers/cpp_report/the_assignment_operator_revisited.html].

This is basic principles, if you don't analyze your class from every angle or you update it and forget to update the copy constructor and/or assignment operator: BAM! memory leak.

I've glowcoded apps that have been running for years at military installations written by "C++ Experts" and found horrendously written code, memory leaks, and mem access violations/seg faults.

It's for that reason that I see more and more companies turning to languages that hold the programmer's hand. Even myself, I put my juniors on UI code in C# or Java before I let them loose on the algorithms in C++, if they fuck up in Java, at least they get a nice exception stack trace and it doesn't pull the whole system down. If you don't know a bit of assembly and you don't know how to traverse memory addresses to find out what your pointers really are referencing, I'm not letting you near a C++ debugger.

Starting with easier languages allows a person to feel what its like to create something and see it work. Starting out with C++ can be very disheartening and confusing unless you have someone around to tell why your code isn't working. This is a classic example:

char* getString()
{
char szStr[5];
szStr[0]='t'; szStr[1]='e'; szStr[2]='s'; szStr[3]='t'; szStr[4]='/0';

return szStr;
}

I've seen this many times in various forms, some compilers will warn you that you are passing an address to a temporary variable - a lot of them don't.

The problem with code like that above is that the access violation (or segmentation fault if you use gcc) almost never ever occurs where you expect it would or indeed even at the same place if you run the program a few times. It's the kind of problem that a beginner programmer would be stuck with for hours or days, and a lot of times they'll just give up.

In my opinion, step number one is to get the syntax down, Java and C# share almost the same syntax as C++ and that in my mind is a good starting point. If you're still getting syntax errors after your second compile you should slap yourself, write your syntax right the first time.

Then focus on OOP principles, once you understand classes and instances of classes you can focus on your memory model, how your compiler assigns space for arrays and objects. This you can't do without a basic understanding of Number Theory, you need to know how to convert between numbers of different bases, and what that means at an electronic level.

Move on to C pointers and then C++ references, you can't understand C++ references if you don't grasp pointers. Once you understand why the following is evil:

char* pchr = 0;
char& rchr = *pchr;

you can start focusing on all the other good stuff; headers and prototyping, macros, structs and C++ classes, member overloading and shadowing, templates, function pointers (I wish other languages had this) and much much more!

Finally, buy and read books on the subject, there's a lot of material available, written by much smarter people than yourself, C++ is not something you do over the weekend (like Visual Basic), it requires effort, patience and understanding. Understanding only comes when you acquire the knowledge from people who have already gone through the process.

Really, it takes years to wrestle C++ to the ground and make it your *****, most of the time it'll ride you like a disease riddled dollar-per-hour whore and leave you feeling sick, vulnerable and guilty that you paid for the experience...

...and wanting for more, because C++ is power, it lets you do what you want and you get a real sense of accomplishment when you get it to do what you want it to.


EDIT: That last bit there wasn't directed at Kaelan, it was directed at all beginner programmers in general.
 

Kaelan

New member
Nov 4, 2007
17
0
0
Well yes, of course it does take quite a while to understand every last bit of the language thoroughly. But that's why you start with C, which is far easier to grasp. The point isn't really to become a complete C++ guru, but to understand what's going on behind the scenes when you program things.

After shooting yourself in the foot enough times, you start first learning *that* you are in fact shooting yourself in the foot when you do certain things (which, as trivial as it sounds, is actually not always easy to tell, especially with languages that "take care of the hard stuff for you"). Then you learn *why* that was indeed shooting yourself in the foot. And then you start understanding what's actually going on and learn to not do it anymore. And so on and so forth.

If you did something like that in a language that quietly "fixed" it for you...well, you're definitely going to be in trouble down the line, because at one point or another it's not going to do what you were actually trying to make it do - and chances are you'll have absolutely no idea why. And at that point it's much harder to correct fundamental mistakes made or misconceptions held.

If you first learn how to handle memory by managing it yourself, it'll be that much easier for you when you go somewhere else (say, Java) and have to deal with garbage collector shenanigans. If you've always had it taken care of for you and down the line suddenly have to deal with a problem that requires knowledge of how the memory is being handled....weellll, then you're gonna have a lot of catching up to do before you actually "get it".

Would Visual Basic be a good starter language? I saw it was advised to at least know some language before attempting C/C++.
The only valid answer to that question is "it depends". What language(s) you should learn will depend on what you're going to program for. If you're going into something where performance is absolutely critical (like, say, programming games) then you're going to want something like C and C++, that gives you full control of what's going on (and the responsibility of keeping managing it all yourself). If you're more interested in web development, Java and Python are good starting points. C# is great for quickly prototyping ideas before writing a large detailed program in some other language, give how easy and quick it is to put something simple up and running. Etc,etc.

I still see the words "syntax error" in my nightmares...a very unhappy two months...
Syntax errors at least tell you where the error is.
It's a lot more fun trying to figure out precisely where in 1500 lines of code, that someone else wrote, it goes FUBAR.
const void &Enso = *static_cast(0);

D: