Is it possible to set a variable to a range of values? PID TVC Attempt
Q: Why is " A0 " a int type ?
Data types for UNO R4 etc
How many integers can be stored in the Array function at a time in Arduino Uno? can I store around 5000 integers in a single array variable?
Hello, i'm just starting in the arduino world, also in the C world ( I only know a little bit of Python )
I'm in the 3 project of the official starter kit book ( in spanish ). They start with the constants, and the code they write is this:
const int sensorPin = A0;
constants are variables that can not change, OK.
const is for declaring a constant, OK.
int is for declaring the type of the data stored in the constant, OK
sensorPin is the name of the constant, OK
= is to assign the value, OK
A0 is the value of the constant , mmmmmm
; this is for telling that is the end of the constant, OK
As far as I know a " A0 " is not a integer, in the Arduino UNO a integers are a range of -32,768 to 32,767 (16-bit) ( int - Arduino Reference ).
So my question is why const int sensorPin = A0; don't give me an error if A0 is not a int ?
On the Arduino (AVR models) an int is 16 bits, not 32 bits. Thus it goes from -32768 to +32767.
That is different from long which is 32 bits.
According to the C language specification, int must be at least 16 bits or longer and long must be at least 32 bits or longer.
It is OK for a compiler to implement int as 32 bits or even 64 bits. It is OK for a compiler to implement long as 64 bits or longer. But it is not allowed for a compiler to implement long as 16 bits.
So when to use which type?
If the values you will be working with can be represented within 16 bits then it's OK to use int. If you need more than 16 bits use long. If you need more than 32 bits use long long.
Do not be caught up with compiler and/or CPU specifics. As mentioned by others, even within the same product range, Arduinos, there are 16 and 32 bit CPUs available. Instead, only trust what the standard guarantees.
The full specification of types in C are:
charmust be at least 8 bitsintmust be at least 16 bitslongmust be at least 32 bitslong longmust be at least 64 bits
Note: It is perfectly legal for compilers to implement char, int, long and long long as 64 bits. This is in fact not uncommon among DSPs.