Notifications
Clear all

It's C++ not C/C++

12 Posts
5 Users
2 Reactions
181 Views
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Introduction

This may be well known but I see evidence of it in a number of posts and feel compelled to mention it.

A misconception about the Arduino IDE is that it uses a C/C++ compiler.

It doesn't.

The Arduino IDE uses a variant of gcc, a compiler that will compile the C++ or C language. The compiler doesn't compile the C/C++ language

There's no C/C++ programming language.

The Arduino IDE compiler default language is C++.

The Arduino IDE can detect a C file (via the file extension) and override the compiler language setting to accept only the C language.

Definitive Test

Prove this to yourself with the following:

Create an Arduino sketch (cpp_only.ino) that has the files

  • cpp_only.ino
  • c_file.c
  • cpp_file.cpp
  • cc_file.cc

Each file contains

#ifndef __cplusplus
#error This is not a C++ compiler
#endif
// ...everything else

Compiling the sketch produces the error only for the c_file.c:

1>...\cpp_only\c_file.c(2,2): error GD8C7B0A7: #error This is not a C++ compiler
1>    2 | #error This is not a C++ compiler
1>      |  ^~~~~
1>

All the other files will successfully compile.

What this means

As a general rule, the sketch .ino file is a C++ file and unless you're using a marked C language file, you're programming in the C++ language. Specifically, it means that the sketch .ino file can freely use C++ features. FWIW, I have never seen a sketch or library that uses C. You have to go to some effort to do so.


The one who has the most fun, wins!


   
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@tfmccarthy

I was never sure the exact relationship was with C++. I assumed it was compiled with a C++ compiler but with includes and installations for i/o not visible in the Arduino IDE. I seem to recall that you could use a template that made it easy to use say SDL library with a C++ IDE compiler like CODE::BLOCKS but never really dug any deeper.

Just to add to or complement your post this is an AI response which may or may not be accurate?

=========================================================

“Arduino C++” isn’t a different language—it’s essentially a simplified, beginner-friendly layer on top of standard C++.

Here’s how they relate:

1. Arduino uses real C++ under the hood
When you write code in the Arduino IDE, you’re actually writing C++ code. It gets compiled with a C++ compiler (usually avr-gcc or similar depending on the board).

2. It hides some complexity
Arduino makes C++ easier by:

  • Automatically adding things like #include statements

  • Letting you skip main() (it creates one for you)

  • Providing simple functions like setup() and loop()

In standard C++, you’d have to write everything more explicitly.

3. It adds its own libraries and functions
Arduino includes built-in functions like:

  • digitalWrite()

  • pinMode()

  • delay()

These are not part of standard C++—they’re part of the Arduino framework that sits on top of it.

4. Slight restrictions depending on hardware
Because Arduino runs on microcontrollers:

  • Limited memory and processing power

  • Some advanced C++ features (like heavy use of STL) may be unavailable or impractical

5. You can still use real C++ features
You can write:

  • Classes

  • Objects

  • Pointers

  • Templates (with some limits)

So your Arduino code is valid C++—just in a constrained environment.


Simple way to think about it:
Arduino C++ = C++ + helper libraries + simplified structure for embedded hardware.

If you learn Arduino, you’re actually learning a practical subset of C++, which makes transitioning to full C++ much easier.

 



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

@robotbuilder 

The main purpose of my post was to address the misconception I've seen in a number of posts on this forum to the effect of, "There's no C++ code here, We use mainly C." The opposite is true. AFAICT, there's nothing but C++. I think it's important for people to know what language they're programming in.

There's nothing to get excited about. You don't have to change how you program. You just need to be aware that it's C++ and you can use it's features.

SDL is an curious case. The author and maintainers are committed to using only C to program in. IIRC, they do provide C++ bindings. I haven't looked at the code closely but I've always had the feeling that it would benefit with C++. ATST, they've done a wonderful job so far so I don't want to "mess with success."

"Is the AI response accurate?"

For the most part, yes but...the differences are less language issues and more program structure issues.

"Arduino uses real C++ under the hood"

This is confused. There's no "under the hood" here. You're using C++ directly. Like saying underneath this post is English text.

"It hides some complexity"

The items are all program structure and not language issues.

"It adds its own libraries and functions"

Again, program structure issues.

"You can still use real C++ features"

That's accurate.


The one who has the most fun, wins!


   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2042
 

Hi @tfmccarthy,

   Please accept my full apologies if anything I wrote was incorrect or offensive, that has never been my intention.

  I don't think I ever suggested that the compiler Arduino uses was expecting anything other than a C++ code, nor that the compiler is limited in its ability to compile C++ code. 

  However, I do feel the latest C++ language definition is complex and lengthy, and may present a steep and foreboding, learning curve for someone coming to programming for the first time.

Bjarne Strousstrap's Fourth edition C++ Programming Language book is listed as having 1376 pages, and is based on C++11 definition (2011 version), whilst W3Schools intro ( https://www.w3schools.com/cpp/cpp_intro.asp) lists 4 subsequent updates, the latest being C++23 (2023 version), which I presume would require an even heavier volume to describe in equivalent detail. (C++26 definition is expected this year, and experimentally supported by GCC.) 

The second edition of Kernighan & Ritchie (C Programming Language) is only 284 pages. 

Thus, my suggestion for anyone starting with Arduino supported modules, who is unfamiliar with C++, is to look to the C subset of C++, for major part of any programme they wish to write, whilst embracing a limited use of classes, especially those helpfully provided as built-ins to the Arduino IDE and in libraries.

I note the W3Schools page just referenced includes:

Difference between C and C++

C++ was developed as an extension of C, and both languages have almost the same syntax.

The main difference between C and C++ is that C++ supports classes and objects, while C does not.

Of course, achieving full competency with entire C++ language will provide a much richer experience, and a natural progression, but I hope it is not an immediate requirement for someone putting together simple projects of the type regularly demonstrated and explained on this website. 

I do not pretend my suggestion is an ideal solution, and I look forward to reading about better alternative recommendations for someone starting with Arduino based projects, who is not experienced in C++ programming.

 

Best wishes to all, and apologies for any offence or confusion I have introduced, Dave



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Posted by: @davee

Please accept my full apologies if anything I wrote was incorrect or offensive, that has never been my intention.

Dave,

You never have to apologize to me. If I led you to believe I was referring to you, then I owe you the apology.

So, let's agree we're both infallible and I'm the more handsome one. 🙂

I will PM you with more.


The one who has the most fun, wins!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

 

"Arduino uses real C++ under the hood"

 

@tfmccarthy wrote:

This is confused. There's no "under the hood" here. You're using C++ directly. Like saying underneath this post is English text.

 

ChatGPT replied:

You’re right to push back on that phrasing—it’s sloppy.

When people say “Arduino uses real C++ under the hood,” they’re usually trying (badly) to communicate that Arduino isn’t a completely separate language. But technically, you are writing C++ directly, not something that merely compiles into C++ behind the scenes.

What is happening is a bit subtler:

  • The Arduino build system wraps your sketch with some extra code before compilation

  • It auto-generates function prototypes

  • It links against Arduino-specific libraries (like digitalWrite, pinMode, etc.)

So your .ino file is essentially treated as a simplified C++ source file, then slightly preprocessed and compiled with a standard C++ compiler (usually avr-g++, arm-none-eabi-g++, etc.).

A more accurate way to say it would be:

“Arduino is a lightweight framework and build system on top of C++ that hides some boilerplate.”

Your analogy works: saying “under the hood” implies a different layer or language, which isn’t really the case. It’s more like C++ with training wheels and some automatic conveniences, not a different engine entirely.

 



   
tfpfau reacted
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Posted by: @robotbuilder

ChatGPT replied:

Hmm. Correct me if I'm wrong, wasn't that the response from ChatGPT? Or was it from a different agent?

I have to confess, I'm suddenly feeling a bit nervous that I'll run afoul of ChatGPT someplace down the line. But that's fine. I'll recover. 😀


The one who has the most fun, wins!


   
ReplyQuote
(@davee)
Member
Joined: 5 years ago
Posts: 2042
 

Hi Tim @tfmccarthy,

  Thanks for your kind replies, including the PMs.

   I am certainly fallible, and whilst we can't actually see each other, I am happy to agree that you are most likely the more handsome.

   I have started to reply to the PMs, but the effort you put in, deserves more thought and research on my part. 

  In summary, I reminded of the optimist, with a half-full glass, and the pessimist, with a half-empty glass. I think any differences between us are of similar magnitude. 

If I don't get too distracted with other stuff, I'll try to contact you by PM soon.

Best wishes and take care, Dave



   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 7 years ago
Posts: 2491
 

@tfmccarthy 

Hi Tim

I will have to resist giving ChatGPT's responses I have already gotten into trouble once before! It is that I have found it so very helpful even if it can give incorrect replies with great confidence. It is a great teacher and has made learning and coding so much faster and easier. It always responds and doesn't fob you off or ignore your questions or put you down so much so I asked it this question:

Have you been programmed to be nice and not give insulting replies that some humans are prone to?

ChatGPT:

Short answer: yes—intentionally so, but not in a fake or sugarcoated way.

I’m designed to be respectful and constructive, which means I avoid insults, personal attacks, or trying to put people down. That said, I’m not supposed to just agree with everything or “be nice” at the expense of honesty. If something is incorrect, risky, or doesn’t make sense, I’ll push back—just without being rude about it.

Humans sometimes use insults to vent, assert dominance, or react emotionally. I don’t have those impulses, so the goal is to keep things useful, clear, and grounded instead.

If you want blunt, direct feedback (without the fluff), you can say so—I can adjust the tone while still keeping it respectful.

 



   
ReplyQuote
tfpfau
(@tfpfau)
Member
Joined: 4 months ago
Posts: 12
 

Posted by: @davee

  In summary, I reminded of the optimist, with a half-full glass, and the pessimist, with a half-empty glass....

Don't forget the engineer who thinks the glass is twice as big as it needs to be...

 



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 502
Topic starter  

Posted by: @robotbuilder

I will have to resist giving ChatGPT's responses I

No, keep them coming! They serve a useful purpose, and we need to refine how we use them. I keep forgetting to use CoPilot to double check my work and kick myself afterwards. And I enjoy the way you poke at ChatGPT. it promotes discussion.


The one who has the most fun, wins!


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 5 years ago
Posts: 2608
 

@tfpfau 

Nor the realist who points out that you've supplied the wrong size glass.


Anything seems possible when you don't know what you're talking about.


   
tfpfau reacted
ReplyQuote