Is it still C/C++ ?

Forgive me I'm halfway through "Getting started with Arduino", and just bought an Arduino rippoff today. I'm fairly average in programming C++ and slightly better in Java. However definitely not an electronics person. I was just looking through the API, and wondering if old C++ will still work for instance.

void setup(){
  //Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  randNumber = random(300);
  Serial.println(randNumber);

  delay(50);
}

I would be inclined to write as:

#include <cstdlib>

void setup()
{
  //Serial.begin(9600);
  //AnalogRead(0);
  srand(time(NULL));
}

void loop()
{
  randNumber = rand % 300 + 1;     // well i would hope, it's been a while since I've done any c++
  Serial.println(randNumber);   // I smell Java not C....

  //delay(50);
}

Will normal C++ libraries/code/APi like this still work, or do I need to learn yet another half-assed language?

The Arduino uses avr-gcc and avr-g++, it as almost all the std libs from C and C++, so it will work.

It certainly is c/c++, but if you think of the language as incorporating the API's you may know and love from Linux/Mac/Windows, then you'll be disappointed. But only because you're thinking is a bit off.

Arduino uses the AVR libc: avr-libc: Modules which you will want to look at for more detailed info.

There are many other libraries available, too, supporting various hardware/software. Look around, there's a good community here for support.

Will normal C++ libraries/code/APi like this still work

yes, or no. It is real C/C++, but you aren't going to have access to the sorts of "system" libraries you'd be used to in a desktop environment. For example:  srand(time(NULL));
There is no "time" call. There is no world-time clock. The closest equivalents (millis(), micros()) are not useful in setup() for initializing the random number generator because they happen immediately after the microcontroller reboots, and would always have a value of approximately zero.

  randNumber = rand % 300 + 1;     // well i would hope, it's been a while since I've done any c++

I'm pretty sure that's wrong (should be at least "rand()" to indicate a function)
rand() still exists as part of the avr-libc library, which is part of the arduino install (although only a subset is documented in the arduino documentation.) avr-libc: AVR Libc
IIRC, there is no libcpp for the avr c++ compiler, so c++ functions that are part of that library rather than part of the language itself may not be present. (I've always been a C programmer, so the subtleties of C++ libraries vs language.) In particular, anything that depends on dynamic memory allocation (including "new") is at best VERY DANGERGOUS when used on a microcontroller with only 2k of data memory.

gcampton:
...wondering if old C++ will still work

The compiler for your "sketch" will be a flavor of GNU g++ that has been built as a cross-compiler named avr-g++

However---and this is important---

Not all of the standard C++ libraries are available.

The Arduino development team has put together an Integrated Development Environment that "protects" beginners from having to learn a lot of boring details about the need for function prototypes and lots of other stuff, and that doesn't necessarily help experienced programmers like yourself. You don't have to include standard library headers (there is no C++ header , for example, and even though there is a C library header <stdlib.h> header you don't have to include it---Arduino does it for you.)

There are a lot of Arduino-supplied library functions that make it easy to get started without having to look at ATmega data sheets to see what bit of what register you have to manipulate to cause pin 19 of your ATmega328p to go high or low. That's what Arduino is all about: Making it easy to get started doing interesting things with simple hardware.

Now, you can use the standard C library function srand(), but there is no standard C library time() function, since the basic Ardino boards don't have a standard way of maintaining a system time consisting of an integer whose value is the number of seconds since midnight on Jan 1, 1970 UTC.

If you build an Arduino-compatible system with a real-time clock chip (or attach a "shield" with an RTC chip), you can use it as a reference for system time, but you will have to use one of the libraries available for your specific RTC chip (DS1307, for example) and you will have to learn the specific API for that. Of course you can write your own class in C++ to allow you to access the chip. There is a "Time" library that a lot of Arduinoites use to create a more-or-less equivalent system time using the RTC as a reference. Again, you can either learn the API for this library or write your own class in C++ to do the deed.

Also, with the Arduino system of libraries there are no stdin or stdout streams. Elementary input/output can be performed with the arduino HardwareSerial class. If your setup function has something like "Serial.begin(9600);" an object of that class, named Serial is linked into your program and serial communications can take place at 9600 bits/second between the Arduino and your workstation over the same USB/Serial converter that is used to upload programs to the Arduino.

The Serial object has functions defined to print out decimal or hexadecimal integer values or characters or other things that you can learn about if you are interested.

Bottom line: You don't have to learn another half-assed language if you don't want to. If the limitations and quirks of the Arduino Integrated Development Environment irritate you (and they irritate me sometimes), you can write programs in standard C and/or C++ and compile them and upload them to an Arduino board if you want to. You will have to learn some limitations. For example: None of the Standard Template Library classes is available. Things like the new and delete operators are not available. Stuff like that.

The ATmega chips that are the hardware basis of the Arduino system have very limited resources (not very much RAM, for example), and even though the avr-gcc compilers and avr-libc library and the Arduino development environment make it possible to program these devices in C++, there are limitations that embedded systems developers must learn to live with.

Regards,

Dave

Thanks guys, got it.

and yeah, http://cplusplus.com/reference/clibrary/cstdlib/

randomNumber =  (rand() % 300) + 1;

Has been a while :stuck_out_tongue:

davekw7x:
Bottom line: You don't have to learn another half-assed language if you don't want to. If the limitations and quirks of the Arduino Integrated Development Environment irritate you (and they irritate me sometimes), you can write programs in standard C and/or C++ and compile them and upload them to an Arduino board if you want to. You will have to learn some limitations. For example: None of the Standard Template Library classes is available. Things like the new and delete operators are not available. Stuff like that.

Dave, I know we can just run a makefile to build and upload stuff. How do you like to do this?

Also, some of these limitations can be worked around. Search the forum for cppfix.h for new and delete implementations. There is a user contributed library called Streaming.h that provides for stream IO for dumping data out a serial port and, in fact, anything tha derrives from print.

Dave, I know we can just run a makefile to build and upload stuff. How do you like to do this?

I'm not sure if you're asking how to do this or how Dave likes to do this :slight_smile:

Either way I can supply a makefile if you want one.


Rob

skyjumper:
...Also, some of these limitations can be worked around...

I was talking about limitations of the Arduino system as supplied (mainly limitations of the avr-libc library). Because of the limited hardware resources of the chips used in Arduino hardware, the library limitations are not unreasonable (in my opinion).

The ability to add functionality is only limited by hardware, and this is due to the fact that everything is built with a "real" C/C++ compiler. You can make your own functions. You can make your own classes. You can use function templates and class templates, etc...

To me, one of the most important and helpful things about Arduino development is the generous, knowledgeable, and very active user community (particularly as seen on this forum).

My whole point was that when approaching the threshold of the Wonderful World Of Arduino (or, in fact, just about any kind of embedded system development environment), before deciding to enter, programmers should be aware of limitations, and they might have to undergo an "attitude adjustment" to have a happy, fulfilling, successful experience. See Footnote.

Regards,

Dave

Footnotes:
"The secret to attaining contentment in life is very simple: Expectations Management."
---davekw7x

"You can't fall out of bed if you sleep on the floor."
---Drunken ex-roommate of davekw7x

There is a user contributed library called Streaming.h

That might be a good example of why C++ constructs are not necessarily a great idea.
I have a sketch that does quite a bit of text output, so I originally wrote it using the "Flash" library (which also implements streaming.) The code got bigger than I expected, and after some study, it looked like streaming was largely responsible. Replacing the streaming calls with simple functions reduced code size from 9470 bytes to 6810 bytes ("use of streaming caused a 39% increase in compiled sketch size.") (ouch.)

#define USEFLASHLIB 0

#if USEFLASHLIB
#include "Flash.h"
#define fp(string) Serial << F(string);
#else
#include <avr/pgmspace.h>
#define fp(string) flashprint(PSTR(string));
void flashprint (const char p[])
{
    byte c;
    while (0 != (c = pgm_read_byte(p++))) {
	Serial.write(c);
    }
}
#endif

void setup()
{
   Serial.begin(38400);

   fp("this is a test");
}

void loop()
{}