C First?

Started by
48 comments, last by All8Up 12 years, 3 months ago

In all honestly, I can only speak for myself here. The learning approach I mentioned earlier was very useful, particularly syntax part. When you have never written a line of code before, syntax is everything, and learning it is one of the first fundamental steps for understanding what you are looking at. And when you switch over to some other language with a familiar syntax, learning that will be also a hell of a lot easier. I probably would have struggled with C++ if it weren't for C.

And yet, you didn't have to learn B in order to learn C. But yes, syntactically understanding one language will usually help in parsing and reading another language, and I have no doubt that your knowledge of C helped you when learning C++.


[quote name='Cornstalks' timestamp='1328671639' post='4910754']
[quote name='AllEightUp' timestamp='1328669158' post='4910744']
First off, learn "C" in the context of "C++".

But... why???
[/quote]
The question is "learn C before C++?", take the baby steps into C++ but learn the core language "FIRST". All the rest of your arguments get the same answer: "because you need to learn the basics first".
[/quote]
I know what the question is, but I don't get what you mean by "learn the core language." What "core language?" C++ is the core language of C++. Not C.


Do so in the context of C++ so you are not learning the wrong things since, as you say, the languages *are* different to a certain degree.

(emphasis mine) No, no, no. Learning C in the context of C++ would be learning all sorts of the wrong things. Nothing (well, okay, there are some things) drives me crazier than seeing someone pretend like C++ is C. Idiomatic C and idiomatic C++ are so massively different it's hard to even say where to start. It's harder to break a bad habit than it is to just make sure you start with the right habit.


The whole pointer thing was just a very common basic missed knowledge that continually comes up, so I suggest learning that specifically if nothing else.

Certainly, and it should be learned, but it can be learned in C++ just as easily as in C, since it's exactly the same in the two languages.

[quote name='Serapth' timestamp='1328710412' post='4910896']

[quote name='Cornstalks' timestamp='1328671639' post='4910754']
[quote name='AllEightUp' timestamp='1328669158' post='4910744']
First off, learn "C" in the context of "C++".

But... why???
[/quote]
The question is "learn C before C++?", take the baby steps into C++ but learn the core language "FIRST". All the rest of your arguments get the same answer: "because you need to learn the basics first". Do so in the context of C++ so you are not learning the wrong things since, as you say, the languages *are* different to a certain degree. The whole pointer thing was just a very common basic missed knowledge that continually comes up, so I suggest learning that specifically if nothing else.
[/quote]
Isn't it kinda like suggesting people learn French before learning English? They share a number of words and use the same alphabet, but are completely different languages. Sure, knowing one will make learning the other a bit easier, but it seems like a downright wasteful use of time.
[/quote]
Pretty much, though I think a better example might be suggesting learning Latin before learning French (seeing as Latin influenced French, kinda like C influenced C++). But they're completely different languages, and if your goal is to learn French, you're better off just learning French. Whether or not you want to learn Latin someday is entirely up to you, but being fluent in French does not require one to start with Latin first.


C++ is not the best language for learning pointers and fiddling with bits - C is better for that.

I'm sorry, but I fail to see why C is better than C++ in this case. I see them as exactly the same, seeing as there is no difference between pointers or bits in C and C++.


C++ is not the best language for learning object-oriented programming- C#/Java is better for that.

I'll agree with that.


C++ is the best language for learning C++.

This is most true.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement

[quote name='AllEightUp' timestamp='1328706617' post='4910880']
[quote name='Cornstalks' timestamp='1328671639' post='4910754']
[quote name='AllEightUp' timestamp='1328669158' post='4910744']
First off, learn "C" in the context of "C++".

But... why???
[/quote]
The question is "learn C before C++?", take the baby steps into C++ but learn the core language "FIRST". All the rest of your arguments get the same answer: "because you need to learn the basics first".
[/quote]
I know what the question is, but I don't get what you mean by "learn the core language." What "core language?" C++ is the core language of C++. Not C.
[/quote]
Learn
int arr[] = { 1, 2, 3 };
int product = 1;
for(int i=0; i<3; i++)
{
product = product * arr;
}
std::cout << product;


Before:
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
int product = 1;
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
{
product = product * (*it);
}
std::cout << sum;


The C-style syntax is easier to pick up than STL C++ with its templates, namespaces and iterators. I'd make an exception for iostreams, since they're much simpler than format specifiers.

There's no reason to immediately push them into using <algorithms> or anything like that, even if it is more 'idiomatic' C++. They're better off ignoring most of the STL until they have a grasp of the basic concepts of the language. Take things one step at a time.

The C-style syntax is easier to pick up than STL C++ with its templates, namespaces and iterators. I'd make an exception for iostreams, since they're much simpler than format specifiers.



Funny, I would exactly reverse what you said ( in my humble opinion ). I find the C++/STL way much more clear than the "C" version, while I find C style IO ( as in printf/scanf ) much more intuitive than streams. Then again, I think operator overloading is the work of the devil, so I never really took to streams.


EDIT: I just want to make it clear, I am not endorsing the use of printf/scanf, both are horrifically fragile and dangerous methods and being sent to the dustbin of history is a good thing. I am simply stating they are a hell of a lot easier to grok than streams.
Of course a true C++ programmer would do it like this:
#include <iostream>
#include <vector>
#include <boost/iterator/counting_iterator.hpp>
#include <algorithm>
#include <numeric>

int main() {
std::vector<int> vec(boost::make_counting_iterator(1), boost::make_counting_iterator(4));
int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
std::cout << product << '\n';
}


</facetious>

I also recommend learning either C first, or the "C++ core language", if that's what you want to call it. smile.png

EDIT: Oh, and the code above should give you an idea of my level of C++: I've used the language for 10 years now, but there are some areas that I don't [and don't want to] really know, like how to implement boost::counting_iterator from scratch.

Of course a true C++ programmer would do it like this:
#include <iostream>
#include <vector>
#include <boost/iterator/counting_iterator.hpp>
#include <algorithm>
#include <numeric>

int main() {
std::vector<int> vec(boost::make_counting_iterator(1), boost::make_counting_iterator(4));
int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
std::cout << product << '\n';
}


</facetious>

I also recommend learning either C first, or the "C++ core language", if that's what you want to call it. smile.png



#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3};
int product = 1;
std::for_each(vec.begin(), vec.end(), [&product](int val) { product *= val; });
std::cout<<product<<std::endl;
}


Edit: Removed numeric include since it's not needed.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

My original answer got rightfully nuked into oblivion.

Yet 15 posts later, all the more helpfully perceived replies gave precisely the same answer:
Q: "Which is better, C or C++?"
R: "Yes"

int arr[] = { 1, 2, 3, 4 };
int product = 1;
for(int i=0; i<3; i++)
{
product = product * arr;
}
std::cout << product;


"Why doesn't this produce 24?"



int arr[] = { 1, 2, 3 };
int product = 1;
for(int i=0; i<4; i++)
{
product = product * arr;
}
std::cout << product;


"Why does this crash? Shouldn't it print 6?"



int arr[] = { 1, 2, 3 };
int product = 1;
for(int i=0; i<sizeof(arr); i++)
{
product = product * arr;
}
std::cout << product;


"Why does this print <garbage goes here>?" or "Why does this crash?"


int arr[5000000];
int product = 1;
for(int i=0; i< WHAT GOES HERE?!?!?!?!; i++)
{
product = product * arr;
}
std::cout << product;


"I want to be able to enter numbers and then when I stop display the numbers entered and then the product. How can I do that with my array?"
which would then be followed by
"Why does this crash immediately when I try and run it?!?!?!??!?!"

When learning C++ you should focus on learning C++. The C++ language is all of it, including the standard library. Pointers are and arrays are topics that can be delayed initially in favor of standard library containers. Get the good habits built, then show them the alternatives. All of the above loop issues are solved by the simple loop:


for(size_t i = 0; i < arr.size(); ++i) {
product = product * arr;
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I think pointers are important because they give you an insight into what's really happening behind the scenes that you wouldn't otherwise get, but yeah - defer them a little, perhaps.

I'm wary of the talk of "idiomatic C++" - surely the objective is to write useful programs that (mostly, at least) work rather than to be particularly idiomatic? Something is also ringing the Fred Brooks/"No Silver Bullet" bell in the back of my brain here too...

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Idioms are considered idiomatic because they are useful patterns to write correct programs. Being wary of idiomatic code is like being wary of worker safety laws.

Idioms are considered idiomatic because they are useful patterns to write correct programs. Being wary of idiomatic code is like being wary of worker safety laws.


No argument about that much, but (and hence the Fred Brooks/"No Silver Bullet" reference) while they may assist in achieving the goal, they don't guarantee it. Saying something like "my program must be correct, it's idiomatic C++" (which is the direction this discussion is going in) is just as dangerous as saying "my program must be correct, it compiled without errors".

Nitpicking I know and admit, but important to be pointed out nonetheless.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement