@Revertron @rayslava gcc will output a warning;
main.c:6:26: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
6 | printf("Number: %d\n"), number;
| ~^
| |
| int
What that line does is calls printf without any argument (which won't cause a crash, as whatever garbage is in memory or in the register will print out) and then writes the value of number to nowhere (gcc will optimize that null operation out if optimizations are enabled).
The x,x; operator is a way to execute 2 or more operations in one line, for example;
printf("Number: %d\n", number), ++number, printf("Number incremented: %d\n", number);
Although you would really write; printf("Number: %d\n", number), printf("Number incremented: %d\n", ++number);
Or actually; printf("Number: %d\nNumber incremented: %d\n", number++, number);
main.c:6:26: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
6 | printf("Number: %d\n"), number;
| ~^
| |
| int
What that line does is calls printf without any argument (which won't cause a crash, as whatever garbage is in memory or in the register will print out) and then writes the value of number to nowhere (gcc will optimize that null operation out if optimizations are enabled).
The x,x; operator is a way to execute 2 or more operations in one line, for example;
printf("Number: %d\n", number), ++number, printf("Number incremented: %d\n", number);
Although you would really write; printf("Number: %d\n", number), printf("Number incremented: %d\n", ++number);
Or actually; printf("Number: %d\nNumber incremented: %d\n", number++, number);
Watch out: unlike the sequencing implied by the comma operator, there's no sequence point between the arguments to a function, and they may be evaluated in any order, or even have their evaluation interleaved.
CC: @rayslava @Revertron
CC: @rayslava @Revertron
@lxo @rayslava @Revertron Thanks, you probably saved me some time there, as I hadn't yet decided to use it in a non-trivial case (so far I've used it do to `++i, ++j;`);
trying to get too clever with side effects and sequence points of little visibility is probably bad practice anyway, except when writing code for obfuscated programming contests
the fact that the comma operator sequences the right operand after the left operand, while the argument separator with the same spelling ',' doesn't, is a somewhat common source of undesirable surprises
CC: @rayslava @Revertron
the fact that the comma operator sequences the right operand after the left operand, while the argument separator with the same spelling ',' doesn't, is a somewhat common source of undesirable surprises
CC: @rayslava @Revertron