-1

What the differences between Arduino language and standard C used for MCU programming (like AVR).I heard Arduino language is C.What happen if i use Arduino language with bare-bones AVR chips.Or with PIC mcu.Will it work? And, if i use just use AVR chip without arduino, will it work with sensor that designed for Arduino (lets say HC-SR04 ultrasonic sensor).And will sensor designed for arduino work with PIC mcu or other non AVR mcu? I'm a beginner mcu programmer and currenly learning Arduino.Which is good starting point learning Arduino first or just go straight to MCU programming (without Arduino, just bare bones chip)? Thank you

10
  • 3
    @m.Alin As far as I know, it supports C++ only. C and C++ are not compatible languages. – Lundin Mar 14 '17 at 7:55
  • 2
    @TomCarpenter Try loading a C file that has int new = 5; in it then ;) – marcelm Mar 14 '17 at 10:50
  • 2
    @TomCarpenter His point is that C is not C++. int new = 5; is perfectly valid C but invalid C++. More relevant, try compiling a C file that has VLAs, designated initializers, flexible array members, the _Generic keyword or other such C-specific features in it. C and C++ stopped being compatible languages somewhere around the year 1995, well over 20 years ago. – Lundin Mar 14 '17 at 12:06
  • 3
    @Lundin if you include a C file in the Arduino IDE, it will treat it as C. Having just tried it int new = 5 in a .c extension file compiles perfectly well... – Tom Carpenter Mar 14 '17 at 12:10
  • 3
    C files are compiled using avr-gcc, C++ files are compiled using avr-g++. The only thing you have to do to mix the two is to have #ifdef __cplusplus extern "C" #endif ... in header files that are shared between the C++ and C codes. – Tom Carpenter Mar 14 '17 at 12:15
7

There's no "Arduino language", it is C++ with some pre-written libraries to provide an abstraction layer on top of the hardware. You cannot use that library on other microcontrollers because it was designed for the specific MCU (AVR).

Pretty much all microcontrollers on the market have compiler support for the C language, which is similar to C++. There is also often C++ compilers available. But overall, C is by far the most popular language to use when programming embedded systems, with the best tool support.

I would probably avoid spending too much time learning about the Arduino library, since that is not useful knowledge for other platforms. Focus on learning C and study at least one assembler (to grasp what happens underneath the hood of C).

5
  • 2
    While it is c++ the libraries aren't quite true c. There is no printf, doubles are only 32 bit floats and I'm sure there are some other oddities. So it's just different enough from c to trip you up. – Andrew Mar 14 '17 at 10:25
  • 2
    @Andrew C doesn't guarantee double to be 32 bit, or even be supported. That's why you have fancy constants in float.h which tell you how big your numbers really are. And printf is part of libc, not the C language. – Dmitry Grigoryev Mar 14 '17 at 15:02
  • 1
    @Andrew: printf() is seldom used on the Arduino platform, but it is supported. You will have to setup your stdout before using it. You could quite easily have stdout print through Serial. – Edgar Bonet Mar 14 '17 at 17:33
  • Given that, why does arduino still work on non AVR MCUs like the ESP8266? – dandavis Mar 14 '17 at 18:28
  • 1
    @dandavis there are Arduino cores for each architecture that all follow a standard API. When you install the ESP8266 hardware package the ESP8266 core library and tools are installed in addition to the hardware definitions and these are used when you compile for any of the boards of that package. The Arduino IDE is certainly not restricted to AVR, though that is currently the most commonly used architecture. – per1234 Mar 14 '17 at 23:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.