Search code examples
cdeclarationc89statements

ISO C90 forbids mixing declarations and code... but allows it in certain instances?


I am using the following flags (where cc is either gcc 4.2 or clang 8.0):

$ cc -Wall -Werror -pedantic -ansi -std=c89 main.c

(I know the -ansi flag is a bit redundant in this case)

The following gives me the expected error

main.c:31:8: warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement]
  vec3 abc = {0};
int main()
{
  vec3 a = {0};
  vec3 b = {0};

  Vec3(2, 2, 2);

  vec3 abc = {0}; // Declared after a function call

  return 0;
}

However, the following does not

int main()
{
  vec3 a = Vec3(0, 1, 2);
  vec3 b = Vec3(0, 1, 2);

  vec3 abc = {0}; // Declared after a function call

  return 0;
}

Surely initializing a variable with a function still counts as mixing declarations and code?

The Vec3 function is very basic; no inline flag set, etc.

vec3 Vec3(float x, float y, float z)
{
  vec3 rtn = {0};

  rtn.x = x;
  rtn.y = y;
  rtn.z = z;

  return rtn;
}

Solution

  • In this code snippet

      vec3 a = Vec3(0, 1, 2);
      vec3 b = Vec3(0, 1, 2);
    
      vec3 abc = {0}; // Declared after a function call
    

    there are only declarations. There are no statements. Function calls used to initialize the variables are expressions. They are not statements.

    It seems this warning

    warning: ISO C90 forbids mixing declarations and code

    is confusing. It would be more correctly to write that

    warning: ISO C90 forbids mixing declarations and statements

    For example even a redundant semicolon introduces a null statement. So in general the compiler should issue a warning even for the following code snippet

      vec3 a = Vec3(0, 1, 2);;
                           ^^^^
      vec3 b = Vec3(0, 1, 2);