All the examples I've found in the net about the usage of getopt_long
(for example
this here) declare the struct option
with the long options as static
.
I don't understand why all these examples declare a static variable, I don't see why this should be done and the man pages don't say anything about this either.
Should I declare this variable as a static variable? And why?
The reason is efficiency. If you declare the struct option long_options[]
not static, it will be constructed at run time upon entry into the main function, but if it is declared as static, it will be constructed at compile time.
But it will work either way.