Hello!
I hope you can help me understand what happens when I use an empty int, for example:
int buttonstate;
what happens when I use this? how will the IDE interprete it?
Thank you!
Hello!
I hope you can help me understand what happens when I use an empty int, for example:
int buttonstate;
what happens when I use this? how will the IDE interprete it?
Thank you!
That declares an int named buttonstate but does not explicitly define its value.
If it is a global variable its value will be set to zero but if it is a local variable then its value will be set to whatever was previously in the memory allocated to it and cannot be relied upon to be a particular value
Did your question arise from a problem that you have had ?
So basically it just reserves memory for an int somewhere. That somewhere depends whether it’s a global or local variable.
In C/C++ there is no such thing as an empty int. An integer variable will always have a value, it can't have no value. As mentioned, if it is a global variable, it will be initialised to zero. If it is a local variable, it might have any value at random, so you should probably initialise it to a known value.
Are int-type and integer-type variables same? The OP talks about int-type variable.
You know very well that they are not the same, but what @PaulRB said is true of integer variables of any kind, indeed variables of any kind in general
With reference to the above, which sketch of the following two should I consider?
int buttonstate;
void setup()
{
}
void loop()
{
}
Or
void setup()
{
int buttonstate;
}
void loop()
{
}
That depends on what you want the scope of the variable to be
Does it imply/mean that integer-type variable do exist?
It implies no such thing
In C/C++ there is no such thing as a an integer data type. However, that does not stop byte, int, long etc being integers
I'd have to disagree (slightly) - there is no data type called "integer", but char, int, long etc are all integer data types
Is the following form of declaration valid?
int buttonstate;
void setup()
{
int buttonstate;
}
void loop()
{
}
I agree with your slight disagreement
What I was trying to express by making data type bold was that there is no C/C++ data type actually named integer but there are, of course, integer data types
Isn't English wonderful !
Yes, it's stupid but valid. You can run a simple test
int buttonstate;
void setup()
{
int buttonstate;
Serial.begin(115200);
Serial.print("in setup: ");
Serial.println(buttonstate);
buttonstate++;
Serial.print("in setup: ");
Serial.println(buttonstate);
}
void loop()
{
Serial.print("in loop: ");
Serial.println(buttonstate);
}
But char, int, long etc refer to integer numbers.
But, this format (being stupid) has proved a useful means to clarify -- what are global and local variables.
I'm unclear about the use of the word "but" there.
My intention is to paraphrase the above as follows (I am a non-native):
I'd have to disagree (slightly) - there is no data type called "integer"; but, they (char, int, long etc.) refer to integer numbers.
float can have the value NaN (Not a Number). I assume this is some bit pattern that a floating point number would never normally take? I guess you could say float can be "empty" when it is NaN. But global float variables are initialised to zero, not NaN, and local float variables are not initialised so could take any random value.
Hopefully nothing bad. If it is global variable it will be declared and initialised to default value which is 0 for an int. if it is not global, it will be reserved place in memory with whatever garbage was in memory at that time so the value of it will be undefined