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?
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.
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.
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.
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.