2

Below code is not working.

int i = {(void) 999; 100;}; 

Adding parentheses will work. WHY?

int i = ({(void) 999; 100;}); 

There is another way to do this type of assignment:

int i = ((void) 999, 100); 

What make them different?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Aison
  • 33
  • 3
  • What do you think you're actually accomplishing with that? Why would you not simply write `int i = 100;`? Or even `(void) 999; int i = 100;`? – John Bollinger Apr 13 '22 at 16:00
  • (void) 999; is just an illusion of any statement, which can be meaningful in code context. – Aison Apr 13 '22 at 16:51
  • Yes, @Aison. And? The question remains: what do you think you are achieving by stuffing code into a variable's initializer that does not contribute to computing its initial value? The second form I suggested in my previous comment, +/- the cast to `void`, would be far more appropriate if the `(void) 999;` must be executed instead of simply dropped. That's in fact why I presented that alternative in the first place. – John Bollinger Apr 13 '22 at 17:10
  • It does not have to be constant "100" assignment. The focus in this question is syntax. Replace "100" by a function that takes int x and return x. By stuffing, you can do something before the function call such as printing another variable's state when assigning. – Aison Apr 14 '22 at 16:15
  • think of an existing function `int getX(int x);` with no access to is implementation. `getX()` is called everywhere but you want to know the state of variable y every time `getX()` is called. "override" getX() by macro definition: ` #define getX(x) ((void) printf(y), (getX)(x))` – Aison Apr 14 '22 at 16:22
  • Well, that's at least plausible. But if you are minded to play *Fun With Macros* then you would probably be better off to write a macro that interposes a wrapper function instead of a comma expression. – John Bollinger Apr 14 '22 at 16:41
  • I use comma expression inside a Macro instead of wrapper function for reasons. For example, to use __FILE__, __LINE__ for debug. Imaging checking the variable state and making an assertion so that you know which file which line goes wrong. – Aison Apr 15 '22 at 17:33

1 Answers1

3

In this declaration

int i = {(void) 999; 100;}; 

there are used two statements inside the braces

(void) 999; 

and

100;

as initializers.

This is syntactically invalid. To initialize a scalar object using a list enclosed in braces you may use only one assignment expression and neither statements.

This construction

int i = ({(void) 999; 100;});

is also an invalid C construction. However a compound statement enclosed in parentheses may appear as an expression in GNU C. It is its own language extension. The value of the expression is 100. That is the variable i is initialized by the value 100.

This declaration

int i = ((void) 999, 100); 

is the only valid C construction. Within the parentheses there is used the comma operator. The value of the expression is the right most operand of the expression that is 100.

In fact the last declaration is equivalent to

int i = 100;

The compiler should issue a warning that the expression ( void )999 has no effect.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335