开发者

Why doesn't GNOME use C99?

Looking at mutter source code and evince source code, both still use C89 style of declaring all variables at the very beginning of the function, instead of where it is first used (limited s开发者_JS百科cope is good). Why don't they use C99? GNOME 3 was launch recently and mutter is quite new, so that could have been a good opportunity to switch, if the reason was compatibility with old code style.

Does that mean that contributing code to GNOME needs to be written in C89?


The rationale can be linked to the same rationale behind Glib and GTK+:

  • No C99 comments or declarations.

Rationale: we expect GLib and GTK+ to be buildable on various compilers and C99 support is still not yet widespread.

Source: http://live.gnome.org/GTK+/BestPractices


Speaking of scope, I guess you can still do this:

if (condition)
{
  int temporary = expression();
  trigger_side_effect(temporary);
}

In other words, each actual brace-enclosed scope can contain new variable declarations, even in C89. Many people seem surprised by this; there's no difference from this perspective between a function's top-level scope and any other scope contained therein. Variables will be visible in all scopes descending from the one that declared them.

Note that I don't know if this is supported by the GNOME style guide, but it's at least supported by C89, and a recommended technique (by me) to keep things as local as possible.


Many people consider declaring variables all over the place, as opposed to at the beginning of the block, bad style. It makes it mildly more work to look for declarations, and makes it so you have to inspect the whole function to find them all. Also, for whatever reason, declarations after statements were one of the last C99 features GCC implemented, so for a long time, it was a major compatibility consideration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜