Arduino 使用 C 还是 C++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11812850/
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
Does Arduino use C or C++?
提问by memilanuk
Coming from Python, the whole C/C++ thing is kind of alien to begin with... and then I see in one place that Arduino uses 'standard' C, and in another that it uses 'standard' C++, so on and so forth. Which is it? My admittedly crude understanding of the difference between the two is that C++ is (roughly) C with classes/objects. How does that affect which language or dialect (C or C++) should I concentrate on learning for use primarily with Arduino?
来自 Python,整个 C/C++ 的东西一开始就有点陌生......然后我在一个地方看到 Arduino 使用“标准”C,而在另一个地方它使用“标准”C++,依此类推向前。是哪个?我对两者之间的区别的粗略理解是,C++(大致)是带有类/对象的 C。这对我应该专注于学习主要与 Arduino 一起使用的语言或方言(C 或 C++)有何影响?
回答by
Arduino sketches are written in C++.
Arduino 草图是用 C++ 编写的。
Here is a typical construct you'll encounter:
这是您将遇到的典型构造:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
...
lcd.begin(16, 2);
lcd.print("Hello, World!");
That's C++, not C.
那是 C++,不是 C。
Hence do yourself a favor and learn C++. There are plenty of books and online resources available.
因此,帮自己一个忙并学习 C++。有大量的书籍和在线资源可供使用。
回答by tiwo
Both are supported. To quote the Arduino homepage,
两者都支持。引用Arduino主页,
The core libraries are written in C and C++ and compiled using avr-gcc
核心库是用 C 和 C++ 编写的,并使用 avr-gcc 编译
Note that C++ is a superset of C (well, almost), and thus can often look very similar. I am not an expert, but I guess that most of what you will program for the Arduino in your first year on that platform will not need anything but plain C.
请注意,C++ 是 C 的超集(嗯,几乎是),因此通常看起来非常相似。我不是专家,但我想你在第一年在那个平台上为 Arduino 编程的大部分内容都不需要任何东西,除了普通的 C。
回答by molyss
Arduino doesn't run either C or C++. It runs machine code compiled from either C, C++ or any other language that has a compiler for the Arduino instruction set.
Arduino 既不运行 C 也不运行 C++。它运行从 C、C++ 或任何其他具有 Arduino 指令集编译器的语言编译的机器代码。
C being a subset of C++, if Arduino can "run" C++ then it can "run" C.
C 是 C++ 的一个子集,如果 Arduino 可以“运行”C++,那么它就可以“运行”C。
If you don't already know C nor C++, you should probably start with C, just to get used to the whole "pointer" thing. You'll lose all the object inheritance capabilities though.
如果您还不了解 C 或 C++,那么您可能应该从 C 开始,以便习惯整个“指针”。但是,您将失去所有对象继承功能。