What language does Arduino IDE use?
- 获取链接
- X
- 电子邮件
- 其他应用
The Arduino IDE primarily uses C++, but with a simplified style that often looks like a hybrid of C and C++.
Here's a detailed breakdown:
Core Language: C++
Arduino sketches are fundamentally C++ programs, but they hide some of the complexity:
C++ Classes: The Arduino core library is written in C++ classes (like
String,Serial, etc.)Object-Oriented Features: You can use classes, inheritance, and other OOP features
C++ Standard Library: You can include and use C++ standard headers
What Makes It Look Different
1. Simplified Structure
Arduino hides the main function and provides two primary functions:
void setup() { // Runs once at startup } void loop() { // Runs repeatedly }
Behind the scenes, this gets translated to proper C++:
#include <Arduino.h> void setup(); void loop(); int main() { setup(); while(1) { loop(); } }
2. Automatic Includes
The IDE automatically includes the Arduino core library, so you don't need to write #include <Arduino.h> in every sketch (though it's there implicitly).
3. Simplified Function Names
The API uses beginner-friendly function names:
pinMode(13, OUTPUT); // Instead of more complex HAL functions digitalWrite(13, HIGH); // Simple, readable syntax delay(1000); // Easy-to-understand timing
Language Features You Can Use
C Subset (all supported):
Variables, data types
Functions, control structures
Arrays, pointers
Structures, unions
C++ Features (all supported):
Classes and objects
Function overloading
Templates
References
Constructors/destructors
Inheritance and polymorphism
Example Showing C++ Features
// This sketch uses C++ features class Led { private: int pin; public: Led(int p) : pin(p) { // Constructor with initializer list pinMode(pin, OUTPUT); } void on() { digitalWrite(pin, HIGH); } void off() { digitalWrite(pin, LOW); } }; // Create objects (C++ feature) Led redLed(13); Led greenLed(12); void setup() { // Object-oriented approach } void loop() { redLed.on(); greenLed.off(); delay(1000); redLed.off(); greenLed.on(); delay(1000); }
Compilation Process
When you click "Verify" or "Upload":
Your sketch gets wrapped with the necessary C++ boilerplate
The Arduino core libraries are included
Everything gets compiled by avr-g++ (a C++ compiler for AVR chips)
The resulting machine code is uploaded to the board
Key Points
Primary Language: C++
Style: Simplified C++ that often looks like C with Arduino-specific functions
Flexibility: You can write pure C, simple Arduino style, or full C++ with classes
Compiler: Uses GCC-based C++ compilers (avr-g++ for AVR boards, arm-none-eabi-g++ for ARM boards, etc.)
So while it's common to say "Arduino uses C/C++", it's most accurate to say it's C++ with a beginner-friendly wrapper that makes embedded programming more accessible.
- 获取链接
- X
- 电子邮件
- 其他应用

评论
发表评论