Is "ISO C90 forbids mixed declarations and code" needed?

if i create a new C project in Builder, and type code like this:

int main() {
  printf ("This is an example\n");
  int ret = 0;
  return ret;
}

it will print a warning for the line int ret = 0; because a variable was declared not at the start of the function.
it comes from this line in the meson.build:

  '-Wdeclaration-after-statement',

in Web’s build configuration this is even an error:

../../../../../../../../../Код/epiphany/src/bookmarks/ephy-bookmark.c:340:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
  340 |     int example = 1;
      |     ^~~

is this needed? i’m sure gnome doesn’t need to compile with previous century compiler versions any more (variables after other statements were added in C99), and code without the restriction is shorter and easier to write and imo to read.

This code style is common in GNOME. I wouldn’t necessarily recommend using it in new projects. Nowadays I’d say best practice is to declare variables at point of use to minimize scope as far as possible. But in a project where all existing code already follows this code style, I’m not sure relaxing the rule would make code more readable.

i’m sure gnome doesn’t need to compile with previous century compiler versions any more (variables after other statements were added in C99)

You should not be so sure, but instead check per project whether they support C99+. It is common, and sadly not peculiar to GNOME, for C codebases to lag behind standardisation by a decade or two. For example, GTK only started allowing C99 at v4.

It’s probably not such a practical concern nowadays, with GCC and Clang being so widely available, but there was a time where people on Sun or BSD or whatever might be running old and/or proprietary compilers that could only do C89. There might still be a few users like that, and they might still in theory want to be able to use GNOME codebases

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.