• Forum
  • Lounge
  • Why I Feel Learning C First is a Waste o

 
Why I Feel Learning C First is a Waste of Time

Pages: 12
A beginner on a third-party chat recently told me that they planned on buying a C book, with plans on moving onto C++

These are the arguments they've given me:
- C is simpler/more elegant
- C is just C++ without OOP
- C++ will "spoil" you
- C++ forces things on you


Let me tackle these individually:
C is simpler/more elegant
I have to admit I chuckled a bit when I heard this. C may be simpler (it has much, much less features than C++ does), but it's far from being elegant.

Here are two examples of C and C++ that do pretty much the same thing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// C++

class Object
{
	public:
		void Output()
		{
			std::cout << x << "\n";
		}
		
		void DoTask()
		{
			x = 0;
		}
	private:
		int x;
};

/////////////////////////////////////////

// C
struct object_t
{
	// Data and stuff
	int x;
};

object_t* CreateObject()
{
	// While allocating memory dynamically here is not necessary,
	// for some reason I've seen (a lot of) C code that abuses it...
	object_t* object = (object_t*)malloc(sizeof(object_t));
	return object;
}

void DestroyObject(object_t* object)
{
	// Do some deinitialization
	free(object);
}

void ObjectOutput(object_t* object)
{
	printf("%i%s", object->x, "\n");
}

void ObjectDoTask(object_t* object)
{
	object->x = 0;
}


This is how you would use them, respectively:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// C++

int main()
{
	Object object;
	
	object.DoTask();
	object.Output();
	
	return 0;
}

// C

int main()
{
	object_t* object = CreateObject();
	
	ObjectDoTask(object);
	ObjectOutput(object);
	
	DestroyObject(object);
	
	return 0;
}


Which looks more elegant to you? Simplicity isn't always better.

C is just C++ without OOP
The person I was arguing with claimed he knew both C++ and C, but this (along with the fact that they were pondering on whether to learn C++ or C first?) definitely showed me that they were a beginner.

Idiotmatic C and C++ are completely different. Sure, your archaic C code will most likely work with little (if any) change, but it's going to be frowned upon by other C++ programmers. This is the same in C. Don't write C like a C++ programmer. This doesn't mean to abuse OOP in C++. i.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Instead of
class Vector2
{
	public:
		int GetX() const{return x;}
		int GetY() const{return y;}
		
		void SetX(int x) {this->x = x;}
		void SetY(int y) {this->y = y;}
	private:
		int x;
		int y;
};

// Just use this
struct vector2_t
{
	int x;
	int y;
};


C++ will "spoil" you
I'm not going to lie, my first reaction was:
"Wut?"
There's a difference between spoiling and having a convenience feature.

Let's say you own a bike shop. You have hired have a human worker. His productivity is low, and you have the choice to replace him with a new machine that can put out 4 times as many bikes as the human worker, and costs less to maintain. Which would you choose? (Excuse my bad analogy, but I think it gets the point across)

C++ forces things on you
...

C++ doesn't force you to do anything. If you want, you can write in pure C and your program will run fine. You don't pay for what you don't use. If you want to write performance-critical code, then restrict yourself to a special subset of C++ if you want. It's not uncommon that not every feature of C++ is exploited in code.

__________________________________________________

I'm going to say this, and I have a feeling that a lot of programmers will disagree with me. By learning C++, you're going to eventually learn idiomatic C if you want to do anything non-trivial. There is a 99.9 percent chance that you're eventually going to interface with C libraries at least once. After you interface with several, you're going to quickly up on idiomatic C (provided they're written that way). It should be trivial to write a C application that's not frowned upon by C programmers once you're a reasonably knowledgeable in C++.

C isn't going to help you at all. It's just a waste of time to learn. Most of your time picking up C++ is going to be spent "unlearning" certain practices when writing C code.

The discussion ended with the beginner angrily leaving the discussion, so I've created this topic in hopes that they'd see it. If you change your mind, send me a PM.

(Sorry for this low-quality post. I'm tired and have to do a few things, I had a bit of spare time so I decided to write this)
Last edited on
It should be trivial to write a C application that's not frowned upon by C programmers once you're a reasonably knowledgeable in C++.

If they are modern C programmers, it's not trivial. It may take some work to get in that mindset and more to learn all the differences. C took a lot from C++, from const and prototypes in C89 to threads and atomics in C11, but it changed things. Bjarne likes to say the only thing C got right were the // comments.

To me, the most important core difference between the two languages is probably C.1.2[diff.basic]3.9: in C, types with different names but identical structure are the same (formally "compatible"), while in C++ they are completely different types. (among other things, this means that much of C code is in violation of ODR when compiled by a C++ compiler)

Besides the differences, there are simply things that C has and C++ doesn't. Short list (not including C11): designated initializers, variable length arrays, flexible array members, static array indices, restricted pointers, compound literals, and completely, fundamentally different complex number math (and the imaginary numbers, if you're an Oracle C user).

In short, don't trivialize C.

I do agree that learning it and immediately un-learning for the purpose of learning C++ is pointless. Something like Haskell would be more helpful, less overlap in the initial stages.
Well, C itself is indeed more elegant than C++. The elegance of a given program will depend on the programmer, but given C++'s greater expressiveness IMO C++ programs have better chances of being more elegant. Certainly more robust, as C++ lets the programmer more effectively prevent many common sources of errors.

I never really understood the idea of wanting to learn C in order to learn C++. Makes as much sense as wanting to learn Latin in order to learn Spanish.

There are valid criticisms one could use to argue in favor learning C instead of -- rather than before -- C++ but those are certainly not them.
1
2
3
4
5
6
7
8
9
10
11
12
class Vector2
{
	public:
		int GetX() const{return x;}
		int GetY() const{return y;}
		
		void SetX(int x) {this->x = x;}
		void SetY(int y) {this->y = y;}
	private:
		int x;
		int y;
};


Tbh that's pretty nasty. You could write global set/get functions in C as well, you'd just be polluting the global namespace though, would introduce type safety issues (at least from C++ perspective). If there is a difference i would say this was it:

1
2
3
4
5
6
7
8
struct Vec2
{
    int x; // getters and setters is something java overuses, this is perfectly fine
    int y;

    Vec2& operator += (const Vec2& a) { x += a.x; y += a.y; return *this; }

};
I think all the evidence you need is college "C++" classes that claim to teach C++ but actually teach C (poorly, at that). C is a great language but it shouldn't be a first language.
personally i prefer learning systems programming in c, because c++ has a much more extensive run time with exceptions and what not. c was also designed to be a systems programming language, whereas c++ is a general programming language, so its kind of like comparing apples and oranges
I'm always surprised when I hear somebody's saying it's a waste of time to learn C. In order to be a competent programmer you must know it, period.

However, learning C specifically to learn C++ seems to be stupid. Those are two quite different languages which share some syntax.

Also, that example of "elegance" of those two languages is a complete fail. Try not to code C++ in C next time. That being said, I agree that you can write more elegant programs in C++ as it is a higher-level language. You can also write unbelievably non-elegant programs in C++ if you're not careful.
c was also designed to be a systems programming language, whereas c++ is a general programming language
C++ is a systems programming language.

I'm always surprised when I hear somebody's saying it's a waste of time to learn C. In order to be a competent programmer you must know it, period.
Although I don't agree with this statement, while learning C++ you will inevitably learn enough C to reach the level of competence you're talking about.
Is no-one going to point out the fallacy of comparing object-oriented C and C++ code when C was not designed for object-oriented programming? It's like saying "Haskell is a better functional programming language than C++ because monads are more elegant in Haskell than C++, therefore Haskell is a better programming language than C++". Each statements is true, but the comparison is unfair. Monads are built into Haskell and OOP is built into C++. You can simulate monads in C++ and OOP in C but it's always going to be less elegant because the language wasn't designed to include those features.
C++ is a systems programming language.

no its a general programming language with a bias to systems programming. http://en.wikipedia.org/wiki/C%2B%2B and there is a youtube video somewhere where bjarne says something similar.

Is no-one going to point out ... include those features.

couldnt agree more. i meant to include that in my post... its completely ridiculous to say paradigm a is better than paradigm b because B doesnt have foo... k
IMO, monads are not elegant -- they contaminate everything they touch. They are necessary, though, in a language like Haskell because of its extremist philosophy on side-effects.
Little Bobby Tables wrote:
no its a general programming language with a bias to systems programming


Psst, take a look at this ... http://en.wikipedia.org/wiki/System_programming_language
yes im aware c++ can be used for systems programming... i never said it couldnt. but calling it a systems programming language would be a mistake
the same can be said for C.
*sigh* no its not. c++ was/is designed as a general programming language with a bias to systems programming. c on the other hand was literally written as a DSL for systems programming, specifically for porting Unix to c, making it more portable than when it was written in assembly. It became popular, so various implementations were made, which is why c was standardized (by ansi iirc, but i cant remember). besides the standard library, not much has brought it out of the realm of being meant for systems programming
It is irrelevant what it was designed for, only what it is used for.

C and C++ are both used for systems programming and they are both general-purpose programming languages. They are not mutually exclusive.

but even so, saying "no its a general programming language with a bias to systems programming" is frankly stupid. It's designed to be biased to systems programming but its not a systems programming language, what madness are you talking.
Duoas wrote:
IMO, monads are not elegant -- they contaminate everything they touch. They are necessary, though, in a language like Haskell because of its extremist philosophy on side-effects.

I disagree. If monads weren't contagious they would be useless to separate pure code from impure. And that separation isn't just philosophy, it has a practical purpose - it improves code robustness and makes lockless multithreading trivial.

IME monadic code in Haskell still tends to be more elegant than equivalent code in an imperative language because of Haskell's expressiveness. It gets ugly when you want to do something that isn't supported by any high-level library, though.
Last edited on
You don't need to know how to use a hammer to use a nail gun. If you don't know what a hammer does, a nail gun seems like magic ( you might be inclined to use it when you should be using glue :) ). You don't take something apart just to use a nail gun.
I think this topic has derailed quite a bit from the original topic title.
The OT is troll bait, so all troll biting is on topic.
Pages: 12