Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Variable Declaration (C)

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?

like image 574
Fiddling Bits Avatar asked Jul 09 '14 16:07

Fiddling Bits


People also ask

Can we declare static variable in C?

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.

What is static declaration in C?

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.

What is use of static variable in C?

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.


1 Answers

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.

like image 109
Andy Thomas Avatar answered Sep 27 '22 17:09

Andy Thomas