Basic data types

The size of certain data types are not same as the size on CPU version of C++.

This is because sizes depend on microcontrollers instruction set

Eg, In a 64 bit CPU, int occupies 4 bytes, but in ATmega 8bit MC, it occupies 2 bytes. The range of numbers it can represent is decreased accordingly

We have to be consious of the resources used, as these data are stores in SRAM, which is only 2KB in ATmega328

1) boolean (8 bit / 1 byte) - simple logical true/false

2) byte (8 bit / 1 byte) - unsigned number from 0-255

3) char (8 bit / 1 byte) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results

4) unsigned char (8 bit / 1 byte) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity

5) word (16 bit / 2 byte) - unsigned number from 0-65535

6) unsigned int (16 bit / 2 byte)- the same as ‘word’. Use ‘word’ instead for clarity and brevity

7) int (16 bit / 2 byte) - signed number from -32768 to 32767.

8) unsigned long (32 bit / 4 byte) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running

9) long (32 bit / 4 byte) - signed number from -2,147,483,648 to 2,147,483,647

10) float (32 bit / 4 byte) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. This will make your sketch longer and slower If you can avoid it, you should.

11) double : Only arduino due can handle double. In other boards, this will be implicitly cast to float

String

It takes two parameters in one of the following syantax,

String(val)
String(val, base)
String(val, decimalPlaces)

val: a variable to format as a String - Allowed data types: string, char, byte, int, long, unsigned int, unsigned long, float, double

base (optional): the base in which to format an integral value decimalPlaces (only if val is float or double): the desired decimal places

Examples of different constructs

String stringOne = "Hello String";                        // using a constant String
String stringOne =  String('a');                          // converting a constant char into a String
String stringTwo =  String("This is a string");           // converting a constant string into a String object
String stringOne =  String(stringTwo + " with more");     // concatenating two strings
String stringOne =  String(13);                           // using a constant integer
String stringOne =  String(analogRead(0), DEC);           // using an int and a base
String stringOne =  String(45, HEX);                      // using an int and a base (hexadecimal)
String stringOne =  String(255, BIN);                     // using an int and a base (binary)
String stringOne =  String(millis(), DEC);                // using a long and a base
String stringOne =  String(5.698, 3);                     // using a float and the decimal places

Arrays

Examples of different constructs

int myInts[6];
  int myPins[] = {2, 4, 8, 3, 6};
  int mySensVals[6] = {2, 4, -8, 3, 2};
  char message[6] = "hello";

Language reference

Help --> Reference