What is this syntax?

i = 1, 2, 3, 4, 5;

it actually assigns 1 i.

***** I wonder if this type of destination is really useful somewhere? *****

Do you know any application of this syntax?

+3
source share
9 answers

This is not a "destination type". The comma operator is bound very weakly than the destination. So you wrote the equivalent:

((((i = 1), 2), 3), 4), 5;

( , , , , assert), , - , 1 5, , , 2,3,4 .

i = 1, code_that_actually_does_something;. , , , "if" "while", , .

+5

, 2 for

for ( i = 0, j = 0; i < 10 && j < 10; i++ ) {
  ..
}
+15

, . C :

(i = 1), (2), (3), (4), (5);

"", - :

i = 2, j = 3, k++;

if() ( ) for() ( ).

+14
+6

while:

while (c = getchar(), c != EOF && c != '\n')
{
+6

, .

(i = 1), 2, 3, 4, 5, = , .

for, , .

+3

, .

, ,

x = 1, y = 2, z = 3;

( C89).

0

: 1 i, 2, 3, 4, 5. 5 ( ).

0

This is useful if you want to create a macro that performs several actions and returns a value as a function:

#define STEAL_MAGIC_HAT(thing1, thing2, cat) \
(smack(thing1), punch(thing2),get_hat(cat))

extern thing_t g_thing1, g_thing2;
extern cat_t g_cat;
...

    hat_t hat = STEAL_MAGIC_HAT(g_thing1, g_thing2, g_cat);
    don(hat);
...
0
source

Source: https://habr.com/ru/post/1722043/


All Articles