What language does Arduino IDE use?

 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 StringSerial, 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:

cpp
void setup() {
    // Runs once at startup
}

void loop() {
    // Runs repeatedly
}

Behind the scenes, this gets translated to proper C++:

cpp
#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:

cpp
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

cpp
// 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":

  1. Your sketch gets wrapped with the necessary C++ boilerplate

  2. The Arduino core libraries are included

  3. Everything gets compiled by avr-g++ (a C++ compiler for AVR chips)

  4. 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.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?