我应该在学习 C++ 之前学习 C 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/598552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:14  来源:igfitidea点击:

Should I learn C before learning C++?

c++c

提问by Ross

I visited a university CS department open day today and in the labs tour we sat down to play with a couple of final-year projects from undergraduate students. One was particularly good - a sort of FPS asteroids game. I decided to take a peek in the srcdirectory to find it was done in C++ (most of the other projects were Java 3D apps).

我今天参观了大学计算机科学系的开放日,在实验室参观中,我们坐下来玩了几个本科生最后一年的项目。一个特别好 - 一种 FPS 小行星游戏。我决定看一眼src目录,发现它是用 C++ 完成的(大多数其他项目是 Java 3D 应用程序)。

I haven't done any C before but I have looked through some C code before. From what I saw in the .cpp code in this game it didn't look very different.

我以前没有做过任何 C,但我以前看过一些 C 代码。从我在这个游戏的 .cpp 代码中看到的内容来看,它看起来并没有太大的不同。

I'm interested in learning either C or C++ but will probably learn the other later on. Is there any advantage to me learning one before the otherand if so, which one?

我对学习 C 或 C++ 感兴趣,但以后可能会学习另一个。是否有任何优势,我一个学习之前,其他如果是的话,哪一个?

采纳答案by Brian R. Bondy

There is no need to learn C before learning C++.

在学习 C++ 之前不需要学习 C。

They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own.

他们是不同的语言。一种常见的误解是,C++ 在某种程度上依赖于 C,而不是完全指定的语言。

Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

仅仅因为 C++ 共享许多相同的语法和许多相同的语义,并不意味着您需要先学习 C。

If you learn C++ you will eventually learn most of C with some differences between the languages that you will learn over time. In fact its a very hard thing to write proper C++ because intermediate C++ programmers tend to write C/C++.That is true whether or not you started with C or started with C++.

如果您学习 C++,您最终将学习 C 的大部分内容,但随着时间的推移,您将学习的语言之间存在一些差异。事实上,编写合适的 C++ 是一件非常困难的事情,因为中级 C++ 程序员倾向于编写 C/C++。无论您是从 C 开始还是从 C++ 开始,这都是正确的。

If you know C first, then that is good plus to learning C++. You will start with knowing a chunk of the language. If you do not know C first then there is no point focusing on a different language. There are plenty of good books and tutorials available that start you from knowing nothing and will cover anything you would learn from C which applies to C++ as well.

如果您先了解 C,那么这对学习 C++ 是一个很好的补充。您将从了解大量语言开始。如果您首先不了解 C,那么专注于不同的语言是没有意义的。有很多好书和教程可以让您从一无所知开始,并且涵盖您从 C 中学到的任何内容,这些内容也适用于 C++。

Please see further reasoning in this answer.

请参阅此答案中的进一步推理

回答by cdonner

I love this question - it's like asking "what should I learn first, snowboarding or skiing"? I think it depends if you want to snowboard or to ski. If you want to do both, you have to learn both.

我喜欢这个问题——就像问“我应该先学什么,滑雪板还是滑雪”?我想这取决于你是想滑雪还是滑雪。如果你想两者都做,你必须同时学习。

In both sports, you slide down a hill on snow using devices that are sufficiently similar to provoke this question. However, they are also sufficiently different so that learning one does not help you much with the other. Same thing with C and C++. While they appear to be languages sufficiently similar in syntax, the mind set that you need for writing OO code vs procedural code is sufficiently different so that you pretty much have to start from the beginning, whatever language you learn second.

在这两项运动中,您都使用非常相似的设备从雪地上滑下山坡,从而引发这个问题。但是,它们也有很大的不同,因此学习其中一个并不能帮助您解决另一个问题。与 C 和 C++ 相同。虽然它们在语法上似乎是非常相似的语言,但编写 OO 代码与程序代码所需的思维方式却大不相同,因此您几乎必须从头开始,无论您第二次学习什么语言。

回答by Dima

I learned C first, and I took a course in data structures which used C, before I learned C++. This has worked well for me. A data structures course in C gave me a solid understanding of pointers and memory management. It also made obvious the benefits of the object oriented paradigm, once I had learned what it was.

我首先学习了 C,在学习 C++ 之前,我参加了使用 C 的数据结构课程。这对我来说效果很好。C 语言的数据结构课程让我对指针和内存管理有了深入的了解。一旦我了解了面向对象范式的含义,它也清楚地表明了面向对象范式的好处。

On the flip side, by learning C first, I have developed some habits that initially caused me to write bad C++ code, such as excessive use of pointers (when C++ references would do) and the preprocessor.

另一方面,通过先学习 C,我养成了一些习惯,这些习惯最初导致我编写糟糕的 C++ 代码,例如过度使用指针(当 C++ 引用会这样做时)和预处理器。

C++ is really a very complex language with lots of features. It is not really a superset of C, though. Rather there is a subset of C++ consisting of the basic procedural programming constructs (loops, ifs, and functions), which is very similar to C. In your case, I would start with that, and then work my way up to more advanced concepts like classes and templates.

C++ 确实是一种非常复杂的语言,具有很多功能。不过,它实际上并不是 C 的超集。相反,有一个 C++ 的子集,由基本的过程编程结构(循环、ifs 和函数)组成,它与 C 非常相似。在你的情况下,我会从它开始,然后逐步发展到更高级的概念比如类和模板。

The most important thing, IMHO, is to be exposed to different programming paradigms, like procedural, object-oriented, functional, and logical, early on, before your brain freezes into one way of looking at the world. Incidentally, I would also strongly recommend that you learn a functional programming language, like Scheme. It would really expand your horizons.

恕我直言,最重要的事情是在您的大脑冻结成一种看待世界的方式之前,尽早接触不同的编程范式,例如过程式、面向对象、函数式和逻辑式。顺便说一句,我也强烈建议你学习一种函数式编程语言,比如 Scheme。它真的会扩大你的视野。

回答by Head Geek

If you decide to learn both (and as other people have mentioned, there's no explicit need to learn both), learn C first. Going from C to C++ feels like a natural progression; going the other way feels like deliberately tying one hand behind your back. :-)

如果您决定同时学习两者(正如其他人所提到的,没有明确需要同时学习两者),请先学习 C。从 C 到 C++ 感觉就像一个自然的过程。走另一条路感觉就像故意把一只手绑在背后。:-)

回答by Steve Rowe

I'm going to disagree with the majority here. I think you shouldlearn C before learning C++. It's definitely not necessary, but I think it makes learning C++ a lot easier. C is at the heart of C++. Anything you learn about C is applicable to C++, but C is a lot smaller and easier to learn.

我将不同意这里的大多数。我认为你应该在学习 C++ 之前学习 C。这绝对没有必要,但我认为它使学习 C++ 变得容易得多。C 是 C++ 的核心。你学到的任何关于 C 的知识都适用于 C++,但 C 更小,更容易学习。

Pick up K&R and read through that. It is short and will give you a sufficient sense of the language. Once you have the basics of pointers and function calls down, you can move on to C++ a little easier.

拿起 K&R 并通读一遍。它很短,会让你对语言有足够的了解。一旦您掌握了指针和函数调用的基础知识,您就可以更轻松地转向 C++。

回答by Adam Hawes

I think you should learn C first, because I learned C first. C gave me a good grasp of the syntax and gotchas with things like pointers, all of which flow into C++.

我觉得你应该先学C,因为我先学了C。C 使我很好地掌握了语法和诸如指针之类的问题,所有这些都流入了 C++。

I think C++ makes it easy to wrap up all those gotchas (need an array that won't overflow when you use the [] operator and a dodgy index? Sure, make an array class that does bounds checking) but you need to know what they are and get bitten by them before you understand why things are done in certain ways.

我认为 C++ 可以轻松解决所有这些问题(需要一个在使用 [] 运算符时不会溢出的数组和一个狡猾的索引?当然,创建一个进行边界检查的数组类)但您需要知道什么在您了解为什么以某些方式完成某些事情之前,它们已经被它们咬住了。

When all is said and done, the way C++ is usually taught is "C++ is C with objects, here's the C stuff and here's how all this OO stuff works", so you're likely to learn basic C before any real C++ if you follow most texts anyway.

总而言之,C++ 的教学方式通常是“C++ 是带有对象的 C,这里是 C 的东西,这里是所有面向对象的东西的工作原理”,因此,如果您在学习任何真正的 C++ 之前,您可能会先学习基本的 C无论如何都要遵循大多数文本。

回答by old_timer

C is a must know language for software engineering. C++, Java, python, are not. You should learn it first without a doubt.

C是软件工程必备的语言。C++、Java、python 不是。毫无疑问,您应该首先学习它。

回答by John T

In the process of learning C++ you will learn most of C as well. But keep in mind a lot of C++ code is not valid C. C++ was designed to be compatible with C code, so i'd say learn C++ first. Brian wrote a great answerregarding this.

在学习 C++ 的过程中,您还将学习大部分 C。但请记住,很多 C++ 代码都不是有效的 C。C++ 旨在与 C 代码兼容,所以我会说先学习 C++。布莱恩写了一个很好的答案

回答by Daniel Daranas

No.

不。

It's generally more useful to learn C++ because it's closer to the most modern OO-based languages, like Eiffel or C#.

学习 C++ 通常更有用,因为它更接近最现代的基于 OO 的语言,如 Eiffel 或 C#。

If your goal is to learn C++, learn modern, standard C++ in the first place. Leave the mallocs aside.

如果您的目标是学习 C++,请首先学习现代的标准 C++。将 malloc 放在一边。



But Steve Rowehas a point...

但是史蒂夫·罗有一个观点......

回答by hillu

Learning C forces you to think harder about some issues such as explicit and implicit memory management or storage sizes of basic data types at the time you write your code.

学习 C 会迫使您在编写代码时更加认真地考虑一些问题,例如显式和隐式内存管理或基本数据类型的存储大小。

Once you have reached a point where you feel comfortable around C's features and misfeatures, you will probably have less trouble learning and writing in C++.

一旦您对 C 的特性和错误感到满意,您在 C++ 中学习和编写的麻烦可能会减少。

It is entirely possible that the C++ code you have seen did not look much different from standard C, but that may well be because it was not object oriented and did not use exceptions, object-orientation, templates or other advanced features.

您所看到的 C++ 代码完全有可能与标准 C 没有太大区别,但这很可能是因为它不是面向对象的,也没有使用异常、面向对象、模板或其他高级功能。