70

I see in one place that Arduino uses 'standard' C, and in another that it uses 'standard' C++, so on and so forth.

Which is it?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
memilanuk
  • 3,522
  • 6
  • 33
  • 39

2 Answers2

86

Arduino sketches are written in C++.

Here is a typical construct you'll encounter:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
...
lcd.begin(16, 2);
lcd.print("Hello, World!");

That's C++, not C.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
23

Both are supported. To quote the Arduino homepage,

The core libraries are written in C and C++ and compiled using avr-gcc

Note that C++ is a superset of C (well, almost), and thus can often look very similar. I am not an expert, but I guess that most of what you will program for the Arduino in your first year on that platform will not need anything but plain C.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tiwo
  • 3,238
  • 1
  • 20
  • 33
  • 4
    C is indeed *almost* a subset of C++. However, non-shitty C code will look very different from non-shitty C++ code. –  Aug 04 '12 at 23:58
  • True, but non-shitty C code will *compile* as C++ (and C programs that are not C++ are shitty). Of course, good C++ code will not artificially and arbitrarily restrict itself to C. – tiwo Aug 05 '12 at 00:03
  • 13
    Still nope. For instance, the [return value of `malloc` shouldn't be casted](http://stackoverflow.com/q/605845/395760) in C, yet in C++ it's mandatory. Now, allowing C++ to link and interact with C code is great, but for that you mostly need to avoid some reserved words and add a conditional (preprocessor) `extern "C"` *to the header*. That's quite a difference though. Also, there's a whole bunch of lovely C99 and C11 features which C++ does not support; why would good C code artificially restrict itself to a subset of C++? –  Aug 05 '12 at 00:10
  • 3
    @tiwo Non-shitty C code most definitely not compiles with a C++ compiler. For example, whenever you use *variable length arrays* it won't compile. Or whenever you use *structure literals*. – fuz Nov 15 '16 at 12:07