Are the following two static
variable declarations equivalent?
1.
static int var1;
static int var2;
static int var3;
2.
static int var1, var2, var3;
More specifically, in case 2, will all variables be static
, or just var1
?
Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.
They're equivalent.
In case 2, all the variables will be static.
The storage class specifier static
applies to all the declared variables in the declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With