Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variable in 'for' loop initial declaration

Tags:

c

static

c99

Would like to know why cannot I declare a Static variable in for loop initialization as shown below,

for(static int i = 0;;)

Compiling the above loop statement code with my C99 standard compiler I see the below error,

error: declaration of static variable ‘i’ in ‘for’ loop initial declaration
like image 270
Sunil Bojanapally Avatar asked Dec 09 '22 10:12

Sunil Bojanapally


1 Answers

C does not allow it

C11dr 6.8.5 Iteration statements 3

"The declaration part of a for statement shall only declare identifiers for objects having storage class auto or register".

(not static)


Typically code would not benefit form being able to have an iterator that is static.


storage-class-specifier:

typedef
extern
static
_Thread_local
auto
register
like image 186
chux - Reinstate Monica Avatar answered Dec 20 '22 16:12

chux - Reinstate Monica