askvity

Is Arduino a C++?

Published in Arduino Programming Language 3 mins read

While often described as using C++, the exact answer is that Arduino uses a variant of the C++ programming language. It is not purely standard C++, but rather C++ with specific additions tailored for the Arduino environment.

Understanding Arduino's Programming Language

Based on the provided reference, the core of Arduino programming lies in C++. However, it's crucial to understand the nuances:

  • Variant of C++: The language is built upon C++, leveraging its structure, syntax, and power.
  • Special Additions: The Arduino environment adds specific libraries, methods, and functions that simplify common tasks like interacting with hardware (e.g., turning on an LED, reading a sensor). These additions make programming microcontrollers more accessible than using pure C++ from scratch.
  • Sketches: Code files in Arduino are called sketches. This term is unique to the Arduino ecosystem and refers to the C++ code you write.
// Example of an Arduino sketch structure
void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

This structure (setup() and loop()) is one example of the special methods added to the standard C++ environment.

The Compilation Process

The reference mentions that when you create a sketch, "it is processed and compiled to machine language." This process typically involves several steps:

  1. Preprocessing: The Arduino IDE (Integrated Development Environment) often adds necessary header files and function prototypes behind the scenes, simplifying your code.
  2. Compilation: The modified code, which is essentially C++, is then compiled into machine code that the microcontroller on the Arduino board can understand.
  3. Linking: The compiled code is linked with necessary Arduino core libraries and other libraries you might use.
  4. Uploading: The final machine code is uploaded to the Arduino board.

This streamlined process, including the added functions and the 'sketch' terminology, is what defines the "variant" of C++ used by Arduino.

Why a Variant?

The choice to use a variant of C++ was likely made to:

  • Simplify Hardware Interaction: Provide easy-to-use functions (like digitalWrite(), analogRead()) that abstract away complex register manipulation often required in pure microcontroller programming.
  • Lower the Barrier to Entry: Make programming microcontrollers accessible to artists, designers, hobbyists, and students who may not have extensive programming or electronics backgrounds.
  • Standardize Development: Create a consistent framework across various Arduino boards.
Feature Standard C++ Arduino (Variant of C++)
Base Language C++ C++
Additions Standard Libraries Standard Libraries + Arduino Core Libraries (special functions/methods)
Code Unit .cpp, .h files .ino (sketches), .cpp, .h
Typical Use General software, OS, Games, Embedded Systems Microcontroller programming, Prototyping, Electronics Projects

In essence, if you know C++, learning Arduino programming is relatively straightforward as you are already familiar with the foundational language. You just need to learn the specific Arduino libraries and the sketch structure.

Related Articles