C++ and Arduino IDE

I believe that Arduino IDE is based on C++ and so i am trying to learn C++.

Would it be correct to say that C++ code will work in Arduino IDE, but Arduino IDE code may not work in a C++ compiler?

No

Whilst the Arduino is programmed in C++ with some extensions to make use of its capabilities it does not, for instance, have an Operating System, a large memory, a keyboard or a screen so not all C++ functionality is available

Equally, the Arduino extensions prevent some code written for it from running in a normal C++ environment

The Arduino IDE uses the GNU C++ compiler, but there are some additional types (e.g. byte bool) defined in header files and it uses a String library that it different that the one in the GNU C++ library

i often compile Arduino code on my laptop using the gnu compiler with .h files the accommodate the difference in the Arduino .h and libraries

i'll develop and simulate code on my laptop before moving it to the Arduino or ESP32. Many Arduino specific functions (e.g. millis(), digitalWrite, Serial.print()) can be "stubbed" out on a laptop

In addition to the above posts, the answer also depends on which Arduino board you're compiling for (specifically the processor). The C++ Standard Template Library is not supported on the smaller 8-bit processors (i.e. AVR), but it is on ARM and ESP32/8266 based boards.

So they are similar but not the same.

I think that the important thing for me is getting my brain around C++ methodology which I think should be the same.

Not all C++ is a good idea on small memory cpu or mcu devices.
But all C is a good fit for those same devices and C is a shorter study.

C strings especially are the part of C that C++ users blow off and have to get advised to learn when their String variables get too hoggy.

Just plain C is plenty for AVR-duinos like Uno, Nano, Mega2560....

Except that something as fundamental to Arduino as:

  Serial.println(x);

Is C++ code, not C.

For someone who has never programmed, the Arduino is a great way to start. And the Arduino IDE is the easiest IDE I've ever installed.

If you have programmed before you can read-through the "basic" language reference quickly. There's not that much to it. (Once you start adding hardware with additional software libraries, there's a lot more to it.)

But if you specifically want to learn C++, a good book or a good tutorial site (and a regular computer) is the way to go. I learned a lot from CProgramming.com but I'm not sure if the tutorials alone would be as useful as a more-structured book.

C++ wasn't my 1st programming language and I got started with the book Teach Yourself C+ in 21 Days, by Jesse Liberty. It's 21 lessons with exercises at the end of each one, and solutions in the back.

None of the example code in the book will run on the Arduino because it expects a standard keyboard, display, and disc storage, all managed in a standard way by the operating system. Even if you add those things to an Arduino they don't (automatically) work with C or C++.
standard.

The thing I've found missing from programming books is the "big picture" of what programming is all-about. They all just seem to jump into the programming language. The 1st programming class I took was the same... It didn't seem too hard but I didn't really understand what I was doing or what the professor was talking about.

In general, I would say programming is "hard". It's not for everybody and professional programmers make more mistakes every day than any other professionals! ...Most of the time they can find and correct their mistakes (bugs) quickly.

I don't think I've ever done object oriented programming on the Arduino so most of my code has been C or "C like".

Standard ANSI/ISO C++ is a HUGE language (the only book I know of that covers the whole language is the published language standard) but there is nothing in the standard language to blink an LED or read the Arduino's ADC, or anything directly hardware-related. Everything in the standard C/C++ language related to the hardware is "abstracted" so your keyboard & display work with the same C++ code no matter what hardware or operating system you have (but the Arduino doesn't have an operating system and all of that standard language stuff does not work).

There is also no mouse, graphics, or sound, or even color text, built-into the "huge" standard language. All of that is handled by additional libraries.

To be clear, the IDE itself is not based on C++; the programs you write for it are C++. An .ino is almost a regular .cpp file; the conveniences it provides are simple. Aside from trivial examples, on every platform -- Windows, Linux, Arduino, etc -- C++ programs use different libraries, and perhaps some variant of commonly used language features as a result. There are more details about those libraries to learn, than the language. But you should start with the latter.

With the caveat that C and C++ are different languages that have diverged, what is the useful subset of "C plus classes" of C++ for Arduino programs? Does it matter if you

  • only use libraries and don't write them for others -- i.e. consumer vs producer
  • use 8-bit AVR vs more capable boards, which use an OS like FreeRTOS

Just to get started, in addition to the core K&R C, you should know

  • nullptr instead of NULL
  • references
  • function overloading (e.g. Serial.println)
  • operator overloading (as a consumer, e.g. String)
  • classes of course, which is a big topic
    • public protected private
    • friend
    • virtual methods and override
  • lambdas

I disagree. I know C, if I was ever to need to specify a language and call myself a programmer, I would call myself a C programmer.

Yes, just the very first things one does with, say, an UNO will expose some C++ syntax and details; most can be ignored, like just get over it and

    Serial.println("Jello Whirled!\n");

And if you use libraries, often written to exploit C++, therefore necessitating some syntax wrangling chops, using such skillz to slavishly follow and then borrow or steal from any example code will suffice.

More important than acquiring any ability to read or write C++ (lambdas? please, no) is coming to grips with the fact that these are tiny processors, with limited resources and a tendency to need to act and react with the real world in real time through push buttons and LEDs and sensors and motors and stuff, no part of which is certain to be covered in Programming 1.01 no matter the language.

Learn C. Top to bottom, from to back… it is a tiny language, but very well designed to punch way above what you might consider to be its weight class.

I've read a few books on some modern languages, usually Chapters 0 through 2 cover a C like beginning, the rest of the book of 15 chapters is interesting stuff I find totally irrelevant in the context of small scale embedded systems coding.

No time really learning C will be wasted, if you ever do move on or grow up to be a C++ programmer. Again, in the context of this kind of programming. And probably not at all, srsly.

I use a few minor features of C++, fun and convenient, but if someone said write that thing there using C I would have zero no problem at all with that "constraint".

a7

No one needs to learn the wide world of C++ to use that.
Learning C to get started does not exclude learning C++ later.

Learning C first to get started breaks the learning curve into a small one before the bigger ++ one while getting the C that C++ needs anyway.

It will be a lot easier on students to learn and use C first. There is less to deal with., less to explain and except for Serial output, most beginner lessons only need C. The book is only finger-thick if that.

Here's an analogy: want to do more than plain math? Learn Algebra after getting past word problems, but recognize the usefulness of word problems and note how you need to be that good before tackling Algebra.
Do you have to? NO! Go ahead, make it harder if you need to!

Good analogy. I wonder if Algebra is the maths to name however… even Calculus or Differential Equations don't seem to be as much more complicated as C++ is compared to C.

a7

It's not. "real" C++ methodology is largely about using C++ features that are not appropriate (and perhaps not even present) on small microcontrollers. Specifically, use of the Standard Template Library is strongly encouraged.

Arduino is closer to C, with occasional use of C++ features that are convenient and not too expensive. "C with Classes", perhaps - you can read some of the controversy here: Learning C++ properly (not C with classes) - Software Engineering Stack Exchange
(BTW, there's an example there of implementing "sort" using "real C++" that I think is REALLY AWFUL. But then, I'm a C/Assembly programmer...)

I tend to check out when topics start taking on a religious / evangelical flavor.

We didn't have PC's in HS. The maths from pre-Algebra on taught me all the rigor and attention to detail I ever needed writing code!

To me, programming is creative writing in logic! My best code feels closer to algorithmic poetry, but that's rare and mostly ended in 2000.

I take things from math to code but I am coding processes not proofs. Maths is #1! Has code ever made you feel like Cantor's infinities?

To me, C++ is as divorced from hardware as they can make it.

And C is close to metal second only to ASM!

With the small environment MCU's, C fits well and C++ .... has some really great features (classes) in toxic-sauce.

When I learned C it validated all that I hated about Basic.

The thing i find difficult in books is often a lack of context.

It's often like having a big list of discriptions of components, without a discription of how components work together. When you look at example programs, you may see the components but they are buried in complexity.

It's a bit like having the description of a nut, a description of a washer and a description of a bolt, and rather than seeing how these components work in a simple machine, you have to try and figure them out by looking at a schematic of an engine.

Are libraries generally written in C or in C++ or a mix? I feel that understanding how libraries work is crucial to knowing how to use them.

are libraries generally written in C or in C++ or a mix?

A mix. For instance, the TFT display libraries I've been looking at are mostly C, but they have a C++ wrapper so that they can be tied to particular displays, and inherit from the Arduino Print class.
The inheritance allows all the printing primitives to work, and all that has to be actually implemented is the write method for drawing a character. Which is pretty neat, and probably a good example of a C++ feature that is useful even on small microcontrollers. (I mean, you can do similar things in pure C, with arrays or structures full of pointers to functions, but it's pretty messy.)

you need to learn 2 things in your first programming course

  • the syntax of the programming language being taught
  • how to program

after learning that, you need to learn other programming concepts

  • the various types of state machines,
  • linked lists,
  • timers in Arduino,
  • different types of data structures
  • object oriented design
  • ...

as well as their improper use, in order to develop efficient programs. it helps to see how larger applications (e.g. OS, compilers, GUIs) are implemented