Arduino static variable declaration with no datatype? [closed]
Arduino static variable declaration with no datatype? [closed]
I am quite new to Arduino and try to understand the following variable declaration:
static btn_state_t nav_btn, joy_btn;
First, I would expect a datatype like integer or float or the like after "static".
I only found one answer that the variable then would be assigned a default type?
Second, I don't understand the following comma-separated "names" or other variables.
In the programm, it looks like they can be both used for "btn_state_t".
I couldn't find an answer to my question so far...or maybe someone can give me a hint on what search-words to look for?
Thanks Steve for the profound and complete answer below! Compleatly answered my question and his guessing was right ...that even with quite some search, I didn't find that datatype and "typedef" was the right codeword.
So solved for me.
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
btn_state_t
nav_btn
and joy_btn
are two static variables of type btn_state_t
. The syntax is similar tostatic int i, j;
– Weather Vane
Jul 1 at 10:14
nav_btn
joy_btn
btn_state_t
static int i, j;
Trying to learn C by trial&error is know to cause depressions. Do not do that. Read a book, take a course ...
– alk
Jul 1 at 11:39
1 Answer
1
Short answer: btn_state_t
is a type, and you're declaring two variables of this type.
btn_state_t
Longer answer:
If you said
int nav_btn, joy_btn;
you would be declaring two variables of type int
. The type name is int
and the names of the variables are nav_btn
and joy_btn
.
int
int
nav_btn
joy_btn
If you said
static int nav_btn, joy_btn;
you would be doing the same thing, but declaring them statically. (This can have two different meanings, depending on whether the declaration is inside or outside a function, but let's not worry about that just now.) In this declaration, the keyword static
is known as a storage class.
static
Once upon a time, things defaulted to int
a lot. Therefore, you could also say
int
static nav_btn, joy_btn;
This has a storage class static
and two variable names nav_btn
and joy_btn
, but no type name. But it ends up doing the same thing, declaring two static variables of type int
, because originally in C, the missing type name defaulted to int
. This style of declaration is obsolete today. But that's what the other answer you found was talking about.
static
nav_btn
joy_btn
int
int
But you've stumbled across another case. You have
static btn_state_t nav_btn, joy_btn;
If there were a comma between btn_state_t
and nav_btn
this would be a declaration of three static variables with their type defaulted to int
. But there isn't a comma, and there's that little clue _t
in btn_state_t
. It turns out that btn_state_t
is the name of a type.
btn_state_t
nav_btn
int
_t
btn_state_t
btn_state_t
So this is a declaration of two static variables, of type btn_state_t
, named nav_btn
and joy_btn
.
btn_state_t
nav_btn
joy_btn
But what's this type name btn_state_t
? Obviously you won't find it in any list of C types, along with int
and long int
and double
and the rest. And the answer is that it's an example of a typedef.
btn_state_t
int
long int
double
Somewhere, in a project-specific header file, or the header file for a GUI or I/O library you're using, is probably a line like
typedef int btn_state_t;
This line defines a new type name. The new type name is btn_state_t
, and the type it names is otherwise identical to type int
. Now when you type
btn_state_t
int
btn_state_t btn;
or
btn_state_t nav_btn, joy_btn;
it's just as if you had said
int btn;
int nav_btn, joy_btn;
In other words, typedef
is sort of like #define
. Saying
typedef
#define
typedef int btn_state_t;
is sort of like saying
#define btn_state_t int
Afterwards, every time we write btn_state_t
, it's just as if we'd written int
. (But in more complicated cases, typedef
can do things that #define
can't, so they're really not at all the same mechanism.)
btn_state_t
int
typedef
#define
Typedefs are a powerful mechanism, but it's always a little surprising to have new type names floating around that you've never heard of, so there's a convention of always using the suffix _t
in a typedef name. Other common examples of such typedefs are type names like size_t
and time_t
and int32_t
.
_t
size_t
time_t
int32_t
If you've never encountered typedefs before, you may be wondering what they're good for. You may be wondering why they're necessary, since at first it looks like good old #define
would work just fine. And you may be wondering what the point is, since you could always just say
#define
static int nav_btn, joy_btn;
if the "real" type behind btn_state_t
is in fact int
. But this answer is too long already, so if you're still curious about typedef
, I'll just encourage you to do a web search on it and read about it.
btn_state_t
int
typedef
btn_state_t
is a type.– Mat
Jul 1 at 10:08