Its been a while I have done pure C programming (I was coding in C++).
Is the following function valid according to standard C?
int main(int argc, char *argv[])
{
int x;
x = 9;
printf("Value of x is: %d\n", x);
int i; /* <==== Is this declaration valid?*/
for(i=0; i < 10; i++)
printf("Value of i is: %d\n", i);
}
Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c >)
the result is same. What I remember from when I was doing C programs
actively was this will give you a compile error.
Please tell me what is the standard about this?
I have one more question
If I declare the variable "i" in "for" loop like this
............... ........
for(int i=0; i < 10; i++)
............... ............... .
Then
1. When I compile with gcc (Command line $gcc <sourcefile.c >) gives me
an error "10: error: 'for' loop initial declaration used outside C99
mode".
2. When I compile with cc (command line $cc <sourcefile.c >) its
compiling and producing the binary executable.
I am bit confused here !?!?
Any comments appreciated.
Thank you for your time.
-Kannan
Is the following function valid according to standard C?
int main(int argc, char *argv[])
{
int x;
x = 9;
printf("Value of x is: %d\n", x);
int i; /* <==== Is this declaration valid?*/
for(i=0; i < 10; i++)
printf("Value of i is: %d\n", i);
}
Its compiling in gcc (ver 3.4.0) and cc (ver 6). For gcc I tried the to
compile with "-x c" and "-ansi" option (gcc -x c -ansi <sourcefile.c >)
the result is same. What I remember from when I was doing C programs
actively was this will give you a compile error.
Please tell me what is the standard about this?
I have one more question
If I declare the variable "i" in "for" loop like this
............... ........
for(int i=0; i < 10; i++)
............... ............... .
Then
1. When I compile with gcc (Command line $gcc <sourcefile.c >) gives me
an error "10: error: 'for' loop initial declaration used outside C99
mode".
2. When I compile with cc (command line $cc <sourcefile.c >) its
compiling and producing the binary executable.
I am bit confused here !?!?
Any comments appreciated.
Thank you for your time.
-Kannan
Comment