2

I'm learning to program in Arduino and, as far as I understand it, it uses the C language (please correct me if I'm wrong).

I'm a senior in JavaScript and PHP, and now I'm having hard time with simple stuff like string handling.

If in JavaScript I have

var c = 33;
var myString = "hello" + c;
alert(myString); //---> hello33

how does it work in C/Arduino?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Francesco
  • 24,839
  • 29
  • 105
  • 152
  • 3
    It's probably best to get [a good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), rather than trying to learn the language from scratch by asking questions on SO... – Oliver Charlesworth Apr 08 '12 at 02:00
  • Specifically, learn about a languages that have stronger typing than JS and PHP. – dreamlax Apr 08 '12 at 02:10
  • 1
    No, it uses [Processing](https://en.wikipedia.org/wiki/Processing_%28programming_language%29) which is similar to C++ and Java – Eelvex Apr 08 '12 at 02:35
  • 3
    No, it uses Wiring, which I believe steals the IDE from Processing but uses simplified C++ as the underlying language :-) – paxdiablo Apr 08 '12 at 03:25
  • No; actually, it uses both :) As in "both three of them" – Eelvex Apr 08 '12 at 04:06
  • 4
    C++. That's all. The Arduino website hides that, but it is true C++ with a real C++ compiler (avr-gcc). Nothing to do with Processing (which is Java based). – Vincent Hiribarren Apr 08 '12 at 18:09
  • 1
    @Eelvex: The first three paragraphs of Wikipia [Arduino](http://en.wikipedia.org/wiki/Arduino) and my own experience support paxdiablo and Vincent - it is basically C++ with some added gimmicks. – A.H. Apr 09 '12 at 09:42

4 Answers4

3

In C, you would do something like:

char buff[100]; // needs to be large enough.
int c = 33;
sprintf (buff, "hello%d", c); // may want a space after hello

// Now do something with buff.

C is a language where you need to manage some low-level details yourself. There's no automatically expanding string type in the base language/library although no doubt there are some good third-party ones around ("better strings", for example, since it doesn't drag in a lot of other stuff you don't want, particularly important in the embedded space).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

Arduino has the built-in String class, and in that you can do:

String stringOne = "A long integer: ";
// using += to add a long variable to a string:
stringOne += 123456789;

// or

stringTwo.concat(123456789);

and if both cases you get "A long integer: 123456789".

tom10
  • 67,082
  • 10
  • 127
  • 137
2

I strongly recommend to use 2 helper libraries. Those together make using strings so much easier!

PString: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236036180 so you can write...

str.print("The temperature is ");
str.print(temp);
str.println(" degrees.");
// do something with str here

Streaming: http://arduiniana.org/libraries/streaming/ So you can write...

lcd << "GPS #" << gpsno << " date: " << day << "-" << month << "-" << year << endl;
Antti
  • 101
  • 1
  • 7
0

The equivalent of your JavaScript code as an Arduino sketch is:

void setup() {
  Serial.begin(9600);
  int c = 33;                 // var c = 33;
  String myString = "hello";  // var myString = "hello"
  myString += c;              //                        + c;
  Serial.println(myString);   // alert(myString); //---> hello33
}

void loop() {
}

For string handling, the differences between the JavaScript and the Arduino (C++) code are:

  • Variables are strongly typed. For example, you need to indicate if a variable is an integer (int) or a string (String) when you declare it.
  • Strings are not built-in 'primitive' types in C++ (and the Arduino version of C++ does not support the C++ standard library string either) so some simple manipulations cannot be performed as a single statement. For example, the assignment and concatenation requires two lines of Arduino code.

Other important differences between your JavaScript and the Arduino (C++) code are:

  • You need both a setup and loop function rather than a main function. setup runs only once when power is applied to the Arduino board and then loop is executed continually.
  • The nearest equivalent of JavaScript's alert in Arduino is to write to the serial port. To see this output you will need the Arduino board to be connected to a PC. Within the Arduino IDE, turning on the Serial Monitor will allow you to see the output.
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127