0

There are several different types in the Arduino programming language. They each have different applications that can use up the limited RAM in the Arduino. When should i use each?

TheDoctor
  • 3,469
  • 1
  • 21
  • 39
  • 2
    Could just be me, but this doesn't seem on-topic. I mean, it's basically an SO question. Maybe a topic for a [meta] question? – hichris123 Feb 14 '14 at 03:26
  • 3
    While I'm inclined to agree that this isn't entirely on-topic, I suppose it's worth noting that the types available using the Arduino IDE aren't exactly the same as regular C++. Notably, the type names boolean, byte and word. are not standard C++. Additionally, the String class is nothing like the C++ std::string. Perhaps a more specific question about the differences is warranted? – Peter Bloomfield Feb 14 '14 at 11:50
  • 2
    I think this question has value as these are not standard types. Explaining how the map from Arduino to avr-gcc is important. – Cybergibbons Feb 14 '14 at 13:20
  • First of all, arduino programmers may not want to go to SO to get their questions answered. second of all, The Arduino has very limited ram, so variable choice based on variable size is important. – TheDoctor Feb 14 '14 at 14:01
  • 1
    The question was in context of specific usage and differences from that when used in Arduino. – mpflaga Feb 14 '14 at 15:50
  • I can understand the practicality of this question based on your answer, but from the question it's not immediately clear to me. Perhaps rewording the title to something like "What are the limits of the data types on the Arduino platform?" – JohnB Feb 14 '14 at 18:32
  • It should probably also be mentioned that no one should use int, long, long long or similar variable declarations anyways. They should be using the types defined in <stdint.h> (e.g. uint8_t, int16_t, etc...), since int does not have a specified size in C/C++, and making compilation behaviour dependent on compiler implementation defined behaviour is irresponsible. – Connor Wolf Feb 17 '14 at 03:49
  • @TheDoctor - If you rewrite this question to be better phrased, it would probably be reopened. Right now, there is no evidence of even understanding what the difference between types. – Connor Wolf Feb 17 '14 at 03:52
  • @FakeName Relying on some of the stdint types for compiler portability isn't always a good idea because they won't necessarily exist. Things like uint32_t will not be defined if the compiler doesn't support a type matching the specified size. Therefore, where exact size is not critical, it may be better to use more generic types. – Peter Bloomfield Feb 20 '14 at 09:57

2 Answers2

5

One should appreciate that the Arduino is an Embedded Processor. Specifically that it has limited resources of RAM and ROM(aka Flash/program space).

Often is the case I see coding use the "int" defaultly. Almost sloppily. If that works great.

BUT!!!! You can run out of room REAL FAST with only 2K of RAM.

I would recommend that you size down your type to the minimum of what it needs to be describing it. One also gets a better understanding of what it really is and its limitations. There is no need for an array of int(16bits) from bytes that were recieved from the SPI, UART or I2C. Or a matrix of RGB's that consume 3 bytes each, being int's. You cut your capacity in half.

The pro/con: PRO: using default int everywhere makes it more inter changeable. The CON is that such interchangeability can lead to unintentional casting errors.

I would also recommend using C99 type def's which can be found at "avr_stdint.html">http://www.nongnu.org/avr-libc/user-manual/group_avr_stdint.html" and elsewhere. It makes things vivid, regards to this aspect. Hence a "int" would be a "int16_t", "unsigned long" be "uint32_t" etc...

It is worth knowing that in some cases a byte or char can not be replaced with int8_t, as such when used with string array functions. But they are still just as vivid.

mpflaga
  • 2,513
  • 13
  • 13
3

An overview of the most commonly used variables:

  • byte: uses one byte (8 bits), holds numbers from 0 to 255
  • char: a signed byte
  • boolean: uses 1 byte, but only holds a value of true or false
  • int: uses 2 bytes, holds numbers from -32768 to 32767, mostly used in general programming. an int is what is known as a signed variable, where the last bit controls if it is negative or positive
  • long: uses 4 bytes, holds numbers from -2147483647 to 217283648, it is also signed
  • long long: uses 8 bytes, can hold -9223372036854775808 to 9223372036854775807, signed
  • float: uses 4 bytes, holds decimals from -3.4028235E+38 to 3.4028235E+38.
  • double: almost identical to float
  • string: an array of chars that represent text
  • String: not to be confused with string, this is a class that holds text data with extra functions

Most variable declarations can be prefixed with signed or unsigned to change the function of the sigh bit. For example, unsigned int x = 0; would create a variable that holds numbers from 0 to 65535 because the sign bit is used as an extra data bit.

TheDoctor
  • 3,469
  • 1
  • 21
  • 39
  • This makes some extremely broad assumptions, and does not list their caveats. All the variable sizes are not specified, can vayry on some platforms/compilers, and is compiler implementation dependent. Furthermore, you're grossly oversimplifying things like floats, which have a large range, but limited resolution. – Connor Wolf Feb 17 '14 at 03:55