In my code I can't initialize variables in for loop initialization part.
When I write this code:
long unsigned int arr[3][3];
char str[50];
for(;gets(str);)
{
int temp=0;
for(int i,j,k=0; str[k]!='\0'; k++){ if(str[k]!=' ')temp=temp*10+(str[k]-48);
the compiler shows
error: 'for' loop initial declarations are only allowed in c99 mode
I have no idea what that means,
but if I write my code like this:
long unsigned int arr[3][3];
char str[50];
for(;gets(str);)
{
int temp=0;
int i,j,k=0;
for(; str[k]!='\0'; k++){ if(str[k]!=' ')temp=temp*10+(str[k]-48);
it works fine.
Why is this happening?
Declaring variables in loops like
for (int i = 0; ...; ...)
was new in the C99 standard, and wasn't allowed in the earlier standards. What the error message tells you is that your compiler is set up to compile using an earlier standard, and so you can't use declarations inside for statements.
You either have to remove the declaration from inside the for statement, or tell the compiler to use a later standard when compiling. Telling the compiler to use a later version can be done by adding the flag -std=c99 if you have GCC or clang.
You are using a compiler that only supports C89, or the compiler is in the mode that supports C89 only. The declarations of variables must in the beginning of a block in C89. It's not a limit anymore in C99 or C++.
Change to C99 mode or put the declaration of i,j,k in the beginning of the block. The way you initialize them looks incorrect, you only initialized k.
for(;gets(str);)
{
int temp=0;
int i,j,k;
for(i=0,j=0,k=0; str[k]!='\0'; k++){ if(str[k]!=' ')temp=temp*10+(str[k]-48);
And don't use gets, it's dangerous, use fgets instead.
This happens because your former code does not obey the C standard under which you compile the code. Check the manual of your C (or C++) compiler how to turn on (if possible) the compilation under the C99 standard. For GNU compiler it is -std=c99 switch.
Related
I'm new to c and I am having a hard time understanding why am I getting an error while trying to compile the following code in c I believe I tried it Java and it worked compiled perfectly without error
void f(void) {
int i;
i = 6;
int j;
j = 20;
}
In "old" C all declarations must be at the top of the functions. In later versions like C99, C declarations can be anywhere in the code. I guess you have an old compiler.
Change your code to
void f(void) {
int i;
int j;
i = 6;
j = 20;
}
The problem is that for some old compilers you need to declare the variables before any executable statement. If you do not want to have this problem, switch to a newer compiler.
If your compiler is configured to compile c code following c98 then you will get this error because following c98 standard the déclaration of variables must be done first then we can make assignment so we can't make declaration of variable in the middle of the code.
However you can select the option to compile your code following the standard c99 and in this case you can make the déclaration of variable in the middle of your code.
#include "stdio.h"
int main()
{
int n;
printf("Enter n:\n");
scanf("%d",&n);
int arr[n][n];
arr[3][3] = 4;
printf("%d",arr[3][3]);
getchar();
return 0;
}
Wasn't using int arr[n], where n is a variable, illegal in C? I am trying to understand what's happening here. Apparently the code works on my clang LLVM compiler and on IDEOne and on Codeblocks. I guess the compiler is just making things easy for me, by doing automatic memory allocation. But another astounding fact is that when I try to set n to 1, 2 or 3 it still works.
Variable length arrays are allowed by C standard since C99. Note that they are still not allowed by C++
standard.
Also important point to note is for versions before c99 and for most C++ compilers variable length arrays are supported by implementations in the form of extensions.
I was trying to do this in ANSI C:
include <stdio.h>
int main()
{
printf("%d", 22);
int j = 0;
return 0;
}
This does not work in Microsoft Visual C++ 2010 (in an ANSI C project). You get an error:
error C2143: syntax error : missing ';' before 'type'
This does work:
include <stdio.h>
int main()
{
int j = 0;
printf("%d", 22);
return 0;
}
Now I read at many places that you have to declare variables in the beginning of the code block the variables exist in. Is this generally true for ANSI C89?
I found a lot of forums where people give this advice, but I did not see it written in any 'official' source like the GNU C manual.
ANSI C89 requires variables to be declared at the beginning of a scope. This gets relaxed in C99.
This is clear with gcc when you use the -pedantic flag, which enforces the standard rules more closely (since it defaults to C89 mode).
Note though, that this is valid C89 code:
include <stdio.h>
int main()
{
int i = 22;
printf("%d\n", i);
{
int j = 42;
printf("%d\n", j);
}
return 0;
}
But use of braces to denote a scope (and thus the lifetime of the variables in that scope) doesn't seem to be particularly popular, thus C99 ... etc.
Now I read at many places that you have to declare variables in the beginning of the code block the variables exist in. Is this generally true for ANSI C 89?
Yes, this is required in the syntax of a compound statement in the C89/C90 Standard:
(C90, 6.6.2 Compound statement, or block)
Syntax
compound-statement
{ declaration-list_opt statement-list_opt }
Declaration have to be before statements in a block.
C99 relaxed this by allowing mixing of declarations and statements in a block. In the C99 Standard:
(C99, 6.8.2 Compound statement)
Syntax
compound-statement:
{ block-item-list_opt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
This is absolutely true for C89. (You're better off looking at documentation for the language, e.g., books and standards. Compiler documentation usually only documents differences between the language the compiler supports and ANSI C.)
However, many "C89" compilers allow you to put variable declarations nearly anywhere in a block, unless the compiler is put in a strict mode. This includes GCC, which can be put into a strict mode with -pedantic. Clang defaults to a C99 target, so -pedantic won't affect whether you can mix variable declarations with code.
MSVC has rather poor support for C, I'm afraid. It only supports C89 (old!) with a few extensions.
Can someone elaborate on the following gcc error?
$ gcc -o Ctutorial/temptable.out temptable.c
temptable.c: In function ‘main’:
temptable.c:5: error: ‘for’ loop initial declaration used outside C99 mode
temptable.c:
...
/* print Fahrenheit-Celsius Table */
main()
{
for(int i = 0; i <= 300; i += 20)
{
printf("F=%d C=%d\n",i, (i-32) / 9);
}
}
P.S: I vaguely recall that int i should be declared before a for loop. I should state that I am looking for an answer that gives a historical context of C standard.
for (int i = 0; ...)
is a syntax that was introduced in C99. In order to use it you must enable C99 mode by passing -std=c99 (or some later standard) to GCC. The C89 version is:
int i;
for (i = 0; ...)
EDIT
Historically, the C language always forced programmers to declare all the variables at the begin of a block. So something like:
{
printf("%d", 42);
int c = 43; /* <--- compile time error */
must be rewritten as:
{
int c = 43;
printf("%d", 42);
a block is defined as:
block := '{' declarations statements '}'
C99, C++, C#, and Java allow declaration of variables anywhere in a block.
The real reason (guessing) is about allocating internal structures (like calculating stack size) ASAP while parsing the C source, without go for another compiler pass.
Before C99, you had to define the local variables at the start of a block. C99 imported the C++ feature that you can intermix local variable definitions with the instructions and you can define variables in the for and while control expressions.
Why does the following code compile with the Dev-C++ compiler and
not with Visual Studio?
Any idea? Here is the code:
#include<stdio.h>
main(){
int n,i;
scanf("%d",&n);
int arr[n];
for(i= 0 ; i <n ; i++)
{
//Do something with the array
}
fflush(stdin);
getchar();
}
Here are the errors:
Errors http://img688.imageshack.us/img688/6618/26863513.jpg
This:
int arr[n];
is invalid because n is not a constant expression. You need to allocate variable sized arrays on the heap using malloc (and then free them when you are done with free).
If you are trying to compile this with a .cpp extension, main must have a return type of int. If you are trying to compile this with a .c extension, then you need to use c-style local variable declaration and declare all of your local variables at the top of the function.
Visual C++ doesn't do stack allocations with that syntax (though I wish it did). You can do stack allocations explicitly with:
int *arr = (int *)_alloca(n*sizeof(*arr));
and no need to free it since it's automatically freed when the scope ends.
This isn’t valid C++ – the Visual C++ compiler does not contain an up-to-date C compiler (rather a C subset of C++) and in particular it doesn’t implement C99 or anything newer. Your code uses features that the Visual C++ compiler doesn’t know (int arr[n]).
To simplify the answers you have gotten:
Your code is C99 and Visual C++ only supports C89. Do yourself a favour and get a better compiler for Windows. The Intel compiler has much better support for C99 than the Microsoft compiler (which has none).
Your program is not a Standard compliant program.
Any standard compliant compiler is required to issue a diagnostic when attempting to compile it.
If Dev-C++ compiled it without a warning, the compiler was invoked in a non compliance mode.
Other than the required diagnostic, a compliant compiler can attempt to compile anyway or just plainly abort compilation.
main()
In C89 this is valid and requires no diagnostic, In C99 this is invalid and requires a diagnostic (valid C99 definitions are int main(void) or int main(int argc, char **argv) or equivalent) ... so if you are using a compliant compiler it is a C89 compiler.
scanf("%d",&n);
int arr[n];
Oops, this is invalid in C89. In C89 you cannot have code intermixed with declarations. A C89 compiler must issue a diagnostic when it sees the declaration of the array.
So ... you are using your compiler in a non-conforming way. There's no way to tell why it compiles or fails to compile.