G
Gary
I have a for loop and try to declare a variable and it complains with
this message.
fun.c:11: error: ‘for’ loop initial declaration used outside C99 mode
line 11: for( int counter = 1; counter <=10; counter++)
This looks illegal I guess with standard C99. So I was looking and
this noob found some information from the C99 document.
---------------------------------------------------
"The declaration part of a for statement shall only declare
identifiers for objects having storage class auto or register."
I am a noob and don't understand some of this language, so I was
looking around in C99 some more.
-----------------------------------------------
"An identifier can denote an object; a function; a tag or a member of
a structure, union, or enumeration; a typedef name; a label name; a
macro name; or a macro parameter. The same identifier can denote
different entities at different points in the program."
"For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a region of
program text called its scope. Different entities designated by the
same identifier either have different scopes, or are in different name
spaces. There are four kinds of scopes: function, file, block, and
function prototype. (A function prototype is a declaration of a
function that declares the types of its parameters.)" If the
declarator or type specifier that declares the identifier appears
inside a block or within the list of parameter declarations in a
function definition, the identifier has block scope, which terminates
at the end of the associated block.
-------------------------------------
If I declare counter like this:
int main (void)
{
int counter;
}
I understand this to have a scope of block. But I didn't understand
what scope I have if I declare in the "for" loop heading (eg. line
11). And I don't understand the storage class auto or register.
Thanks for any insights.
this message.
fun.c:11: error: ‘for’ loop initial declaration used outside C99 mode
line 11: for( int counter = 1; counter <=10; counter++)
This looks illegal I guess with standard C99. So I was looking and
this noob found some information from the C99 document.
---------------------------------------------------
"The declaration part of a for statement shall only declare
identifiers for objects having storage class auto or register."
I am a noob and don't understand some of this language, so I was
looking around in C99 some more.
-----------------------------------------------
"An identifier can denote an object; a function; a tag or a member of
a structure, union, or enumeration; a typedef name; a label name; a
macro name; or a macro parameter. The same identifier can denote
different entities at different points in the program."
"For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a region of
program text called its scope. Different entities designated by the
same identifier either have different scopes, or are in different name
spaces. There are four kinds of scopes: function, file, block, and
function prototype. (A function prototype is a declaration of a
function that declares the types of its parameters.)" If the
declarator or type specifier that declares the identifier appears
inside a block or within the list of parameter declarations in a
function definition, the identifier has block scope, which terminates
at the end of the associated block.
-------------------------------------
If I declare counter like this:
int main (void)
{
int counter;
}
I understand this to have a scope of block. But I didn't understand
what scope I have if I declare in the "for" loop heading (eg. line
11). And I don't understand the storage class auto or register.
Thanks for any insights.