2

With gcc, the following is a valid C statement:

foo ({bar(1); baz = 2;})

Which is equivalent to

bar(1);
baz = 2;
foo(2);

Edited: Here are my (hopefully more informative) questions:

  • Was the ISO C standard committee/body petitioned to make these part of the C standard?
  • If it was, was it before C99? Before C11? Later?
  • What were the reasons given or suggested for not adopting this as part of the language?
  • Is there some large pitfall or drawback to using this construct which I'm not aware of?
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 6
    "Why is this not in the language standard" – because the standards committee decided so. – The Paramagnetic Croissant Nov 12 '15 at 15:42
  • 3
    While I don't see a pitfall immediately, I also don't see a large benefit from using it either... The second code block to me seems clearer and more readable, so why not just use that? – R_Kapp Nov 12 '15 at 15:42
  • 2
    Take a look at the Linux kernel code. I don't have an example at the moment, but there are things that can only be implemented as macros, and this is how you make such a macro "return" a value. – Jonathon Reinhart Nov 12 '15 at 15:43
  • The pitfall is that it is not standard! :-) Standards are more about minimum guarantees when porting your code; otherwise you can use whatever your compiler vendor implements – Luis Nov 12 '15 at 15:43
  • Could you please show the entire context of the code with that expression that you say compiles with `gcc`? When I try it, I get, `error: expected expression before ‘{’ token`. And is `foo` a function or a macro? – lurker Nov 12 '15 at 15:46
  • 3
    @R_Kapp: Allow "returning" values from macros, instead of using an additional macro argument as the return value. – einpoklum Nov 12 '15 at 15:55
  • For reference [expression statement are covered here](http://stackoverflow.com/q/1238016/1708801) ... most likely it is not part of the standard because no one proposed it. – Shafik Yaghmour Nov 12 '15 at 16:00
  • @TheParamagneticCroissant: Rephrased my question. – einpoklum Nov 12 '15 at 20:35
  • You've got a syntax error there? – Johannes Schaub - litb Nov 12 '15 at 20:37
  • The question and the statement-expr feature is interesting in C (for C++, the semantics is less obvious or less intuitive, e.g. because of destructors...). I guess that the answer to *why was it not standardized* is a matter of politics and lobbying. At some time the GCC community could not afford the heavy lobbying and work. – Basile Starynkevitch Nov 12 '15 at 20:45
  • 1
    @BasileStarynkevitch: Did it even try? What were the replies? Don't just say "oh, it's all politics and lobbying" - what were the politics involved? Did someone lobby against this? – einpoklum Nov 12 '15 at 20:47

0 Answers0