Data type with L to the final

Hi there.
I wanna ask you for help. I've been developing a project of a potentiostat to electrochemistry, I've build the physical part, but I am having a problems in the code, another projects related using the data type long used this way:

long t = (5000000L)/(256L*vb) 

t is used to be put in the function delay(t) and vb is the scan rate and can be any number between 1 to 1280

My question is: why they use that data type with L instead data type int, isn´t it the same use int and long with the las character L?
What is the difference?

If you know some article related, please give me.

Thanks in advance :slight_smile:

The long data type is described here
Long data type

And the int data type here
int data type

You will see the long data type is longer than the int data type, so it can hold bigger numbers.
The L is used because numerical constants are int by default.

So the only difference is a bigger range between both data types?

For instance, if I would want to give a delay of 1 second in terms of long it would be:

delay(1000L);

Yes. They also take up more program space as well.
Note the actual number of bytes used by both data types might be different on different processors.

Yes, but if you look at the definition of the delay function it is expecting an int not a long. The compiler should give you a warning but will probably do the conversion for you. There is zero point in sending a long variable to a function that expects an int because it will not actually give you to a number bigger than an int would be.

1 Like

Ok ok, It seems a non-sense (use that data type long) instead int. Don't you think?

In C (and C++), intermediate calculations are performed with "int" type values, assuming that the arguments are ints (which includes constants.) On an AVR, ints are only 16 bits.

In your example:

long t = (5000000L)/(256L*vb) 

The "L"s are unnecessary, because 5000000 is already a long int, and all the calculations would be performed with Longs anyway.
However, if you had a calculation like:

  long t = 1000 * 60 * 5;  // 5 minute interval

You would not get correct results.
This is a VERY common mistake in Arduino programs, mostly because CPUs with a 16bit "int" have become pretty rare (on most modern CPUs, both "int" and "long" are 32bits. (except when "long" is 64bits!))
Search the forum for "integer promotion" and you'll find lots of examples.

1 Like

The value of any calculation is first determined on the right hand side of the =.

Consider

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

  long x = 10 * 5000;
  Serial.println(x); 
}

void loop()
{}

The answer is obviously 50000... and x is big enough to hold 50000... but actually it returns...

21:09:49.824 -> -15536

and the reason is the on the right hand side of the = ... the default type is int (max 32,767)... so it overflows.

If you explicitly state the the variable on the right hand side is "L"ong... then it will work as expected.

Are you sure about that?

1 Like

So, in your example is necessary the "L" to get the correct answer.

why you say that the L is unnecessary? then the problem is to do operations with out set L

long t = 1000 * 60 * 5

as in the case above? which the result is a long type?

Well I was since I looked it up at one time. Looks like it has changed somewhere along the line. This could explain why successive releases of the IDE produced code that took up more memory.
Some code I had over 12 years ago will no longer compile as they are short of memory.

You must have been looking at something else, perhaps delayMicroseconds(). delay() has always taken unsigned long.

(delayMicroseconds() took unsigned long in version 0002, it was changed to unsigned int in version 0003.)

Yes it is... try it yourself.

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

  long x = 10 * 5000;
  Serial.println(x); 

  x = 10L * 5000;
  Serial.println(x); 

}

void loop()
{}

returns...

07:36:56.357 -> -15536
07:36:56.357 -> 50000

Thanks guys for your answers they have been greatly benefited for me. The best kinds.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.