Question about data types

Okay, so I’m currently working on a project using the BMP085 barometric sensor, and I’ve found a nice tutorial over at Sparkfun (BMP085 Barometric Pressure Sensor Quickstart - SparkFun Electronics) which helps me complete it, but there are some things that I don’t fully understand. Several places they use the term “(long)”, “(float)” or “(unsigned long)” in front of some variables some places but not consistently, even though these variables have been defined as such further above. What’s the reason for that? On the reference page they use float(variable), long(variable) etc, is it just the same as mentioned above?

Also they use the term “while(Wire.available() < 3)” without the curly brackets, how does that work? On the reference page on arduino.cc they always use it with the curly brackets, still the same?

Adding a variable type in front of a variable in brackets is called “type casting”. It is how you convert a variable from one type to another in C and C++. The float(…) etc functions provided by Arduino are just an abstraction of this functionality and are non-standard.

Any flow-control statement (while, if, etc) without curly brackets will affect the following line only. For instance:

while (Wire.available() < 3)
  delay(1);

is the same as:

while (Wire.available() < 3)
{
  delay(1);
}

and,

while (Wire.available() < 3)
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);

is the same as:

while (Wire.available() < 3)
{
  digitalWrite(7, HIGH);
}
delay(1);
digitalWrite(7, LOW);

There is also:

while (Wire.available() < 3);

(note the semicolon), which is the same as:

while (Wire.available() < 3)
{
  // do nothing
}

Moderator edit: Attempted to match-up code tags. I hope it worked. AWOL

Several places they use the term “(long)”, “(float)” or “(unsigned long)” in front of some variables some places but not consistently, even though these variables have been defined as such further above.

It is called casting, and it converts one data type into an other temporarily for use in calculations.
So suppose you want to divide two integers and put the result into a float.
V1 and V2 being ints and result being float

result = V1 / V2; // will give a int division and then put the result into a float

result = (float) V1 / (float) V2; // will give you a floating point division.

majenko:
Adding a variable type in front of a variable in brackets is called “type casting”. It is how you convert a variable from one type to another in C and C++. The float(…) etc functions provided by Arduino are just an abstraction of this functionality and are non-standard.

Any flow-control statement (while, if, etc) without curly brackets will affect the following line only. For instance:

Thanks, that was very helpful!

In the code I referred to they use the version with the semicolon, but without curly brackets:

Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2) ;
  msb = Wire.read();
  lsb = Wire.read();
  
  return (int) msb<<8 | lsb;

Does that mean it will do nothing (because of the semicolon), untill it has received 2 bytes, and when it eventually does receive 2 bytes it will release the while loop and execute the following lines?

Other places I’ve seen an “if-loop” being used instead, is it basically the same or does one have an advantage over the other?

Grumpy_Mike:

Several places they use the term "(long)", "(float)" or "(unsigned long)" in front of some variables some places but not consistently, even though these variables have been defined as such further above.

It is called casting, and it converts one data type into an other temporarily for use in calculations. So suppose you want to divide two integers and put the result into a float. V1 and V2 being ints and result being float

result = V1 / V2; // will give a int division and then put the result into a float

result = (float) V1 / (float) V2; // will give you a floating point division.

Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?

I've seen an "if-loop" being used instead,

An "if" by itself does not (cannot) loop.

AWOL:

I've seen an "if-loop" being used instead,

An "if" by itself does not (cannot) loop.

You're right :), what I meant is that an if-statement is sometimes used, for instance here: http://arduino.cc/en/Tutorial/SFRRangerReader.

Do they basically give the same results?

Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?

Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.

Do they basically give the same results?

It depends on the context.

void loop ()
{
  while (Serial.available () <= someNumber) ;  // do nothing. Block "loop()"
  doSomething ();

vs.

void loop ()
{
  if (Serial.available () >= someNumber) // don't block "loop()"
  {
    doSomething();

Grumpy_Mike:

Thanks, but is for instance "(float) V1 / (float) V2"; the same as "float(V1) / float(V2)" ?

Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.

It's standard C++ but not standard C.

The code below compiles in C++ but not C:

void foo ()
  {
  float bar = float (5);
  }

In C++ basically you are calling the constructor for type "float" and supplying it with the argument "5". C doesn't support constructors so that syntax isn't valid in C.

Grumpy_Mike:

Thanks, but is for instance “(float) V1 / (float) V2”; the same as “float(V1) / float(V2)” ?

Yes they are the same, but the latter is not standard C it uses functions pre defined in the arduino IDE.

Okay, I guess it’s up for personal preferences then, but if I ever find myself attempting to program something in C, then I probably should get used to using standard C also when working with arduino IDE?

The Arduino IDE uses C++, unless you happen to make an extra tab, and name it with a .c extension.

[quote author=Nick Gammon link=topic=183280.msg1358242#msg1358242 date=1376905410] The Arduino IDE uses C++, unless you happen to make an extra tab, and name it with a .c extension. [/quote]

Okay. That's probably way ahead of my level anyway :)

People have personal preferences and styles.
My preference is to always explicitly use the curly brackets because using them avoids simple mistakes.

As an example in the code below the indenting can easily fool you into thinking that all three statements are within the scope of the while loop when only the first is.

while (Wire.available() < 3)
  digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);

radman:
People have personal preferences and styles.
My preference is to always explicitly use the curly brackets because using them avoids simple mistakes.

As an example in the code below the indenting can easily fool you into thinking that all three statements are within the scope of the while loop when only the first is.

while (Wire.available() < 3)

digitalWrite(7, HIGH);
  delay(1);
  digitalWrite(7, LOW);

Which is exactly the reason I wrote it like that - to highlight the fact that the indenting is completely meaningless to the structure of the program.

Which is exactly the reason I wrote it like that - to highlight the fact that the indenting is completely meaningless to the structure of the program.

In C/C++ but it's quite significant in python for example.

RoyK:

Which is exactly the reason I wrote it like that - to highlight the fact that the indenting is completely meaningless to the structure of the program.

In C/C++ but it's quite significant in python for example.

And COBOL, which I still have nightmares about - forced to learn it at uni...

Which is exactly the reason I wrote it like that

It happens a lot by accident as well. People start with a single indented line and no brackets. The code works fine but when modifications are required further indented lines are added (without adding brackets) and not enough testing done. At a later date there is a bit of head scratching and its easy not to see the problem.

Another thing I often do is:

while (condition) {
  continue;
}

Which makes it obvious what is happening.

radman:

Which is exactly the reason I wrote it like that

It happens a lot by accident as well. People start with a single indented line and no brackets. The code works fine but when modifications are required further indented lines are added (without adding brackets) and not enough testing done. At a later date there is a bit of head scratching and its easy not to see the problem.

That is why I write just a single statement after an if on the same line. That way there is no way of forgetting what you have done.

if(somethingIsTrue) digitalWrite(pin,LOW);

Otherwise I always write the opening and closing braces before any code is put it. It works for me and I have never made that mistake even years after the code was originally written.