1

I know GCC and probably Clang accept this syntax, but if I'm making games for the common 5 platforms (Mac, Windows, Linux, iOS, Android(Java)) but if I get C to run on Android probably by bridging Java and C, how portable is this? Should this be avoided?

I know that this is a bad funciton because simply return num*num; is enough but this syntax, should I avoid it?

int square(int num) {
    int x = ({
        int y = num;
        y*y;
    });
    return x;
}
  • I'm pretty sure that's a GCC extension. – Barmar Aug 18 '20 at 19:28
  • What's wrong with just `int x = num * num;`? – Barmar Aug 18 '20 at 19:28
  • @Barmar I know, but that's not the point of the question. –  Aug 18 '20 at 19:28
  • How could one answer the question “How portable is…”? There is no metric for portability. What would you want as an answer—“40% portable”? “Portable anywhere GCC and Clang are available?” “Can be ported to strictly conforming C with 7.2 hours of engineer labor”? That aside, the example shown has no reason to use GCC’s statement-expression extension, so why bother? If there is a good reason to use it, you should construct an example showing that reason, and then advice could be given about migrating it to strictly conforming code. – Eric Postpischil Aug 18 '20 at 19:29
  • You should avoid that syntax. It's not portable, and its semantics are idiosyncratic (and not standardised). – rici Aug 18 '20 at 19:30
  • @EricPostpischil Should I avoid it if I want to support Mac, Android, Windows, Linux, and iOS? –  Aug 18 '20 at 19:30
  • If you want something like that, use C++ which has lambdas :-) – rici Aug 18 '20 at 19:31
  • @defineMACRO: If you are always going to use GCC or Clang to compile for those targets, and you are comfortable with using GCC and Clang features and comfortable committing anybody who will work on your code in the future to understanding them in your code, go ahead and use any GCC or Clang features you want. There is no law requiring people to use strictly conforming C, and recommendations to do so are solely based on benefits of portability and familiarity among programmers. If you do not want those benefits, you do not need to adhere to such recommendations. – Eric Postpischil Aug 18 '20 at 19:33
  • @EricPostpischil I _want_ to use GCC, but I'm not sure GCC can compile for Windows. –  Aug 18 '20 at 19:34
  • @defineMACRO: Then you need to investigate the quality of GCC for use in building Windows targets e.g., linking with Microsoft Windows services if that is what you want to do. I cannot speak to that, but I expect others can. – Eric Postpischil Aug 18 '20 at 19:40
  • Of interest: [Are compound statements (blocks) surrounded by parens expressions in ANSI C?](https://stackoverflow.com/q/1238016/6865932) – anastaciu Aug 18 '20 at 19:49
  • You can [use GCC natively on Windows](http://win-builds.org) or even cross-compile to Windows from a Linux distro, macOS, Cygwin, etc. (seek a package with "mingw64" or "mingw-w64" in it), but you must remember that Windows is still not a Unix-like environment, so you may encounter portability issues. If you want to avoid Windows, you may consider [Cygwin](http://cygwin.org), a Unix-like environment on Windows, and anything compiled using Cygwin's native GCC (not the "mingw64" GCC stuff) will require you to distribute the Cygwin DLL file with your compiled binaries. – MemReflect Aug 18 '20 at 21:51

1 Answers1

5

This is not standard C. It's a GNU C extension called Statement Expressions. You should avoid it if you want maximum portability.

Barmar
  • 741,623
  • 53
  • 500
  • 612