C89 (C90, ANSI-C) does not allow intermixing variables declaration with code. I wonder to what extent a variable initialization is considered "code".
Perhaps it's only valid to initialize with constant expressions?
Specifically, if I'm writing C code and I want to play safe (maximize compatibility with ANSI-C compilers), should the following be considered safe?
void f1(void) {
int x = 30;
int y = 40;
int z;
/* ... */
}
void f2(void) {
int x = 30, y = 40;
int z;
/* ... */
}
#define MYCONST (90)
void f3(void) {
int x = 3;
int y = 4 + MYCONST;
int z;
/* ... */
}
void f4(void) {
int x = 3;
int y = time(NULL);
int z = 10 + x;
/* ... */
}
C89 does not allows mixing declarations and statements in the same scope. We are referring here to declarations and statements inside functions as statements are not allowed outside functions in C.
int a = foo(a, b); /* declared at block-scope */
The line above is a (valid C89) declaration, not a statement so all your functions f1, f2, f3, and f4 are valid C89.
However you are allowed to mix declarations and statements in different scopes:
void bar(void)
{
int i = 0; /* declaration */
i++; /* statement */
{
int j = 0; /* declaration */
j++; /* statement */
}
}
The function above is valid C89.