I just noticed that it's possible in C to run functions by placing them in round brackets separated by commas. I wonder whether it is equivalent to just running them normally (without outside round brackets, separated by semicolons) and where this syntax or behavior originates from (was this added as a feature, does this have a special name, are there wanted side-effects, is this actually used etc.). It's quite intriguing how I write this sequence thousands of time a day for many years as part of function calls but I can't remember having seen this standalone even once.
example.c
#include <stdio.h>
int main(int argc, char *argv[])
{
(printf("Hello "), printf("World!\n"));
return 0;
}
Thank you very much for lending your knowledge, Moritz
where this syntax or behavior originates from
Have a look at the comma operator.
printf("Hello ")
is evaluated at first, but its results are discarded.
printf("World!\n")
is evaluated next, and its return value decides the return value of the whole expression
(printf("Hello "), printf("World!\n"));
Meanwhile, you can see "Hello World!" printed to the console which could have been done in a less complicated way:
printf("Hello World!\n");
does this have a special name
I would call it grouping statements using the comma operator.
are there wanted side-effects
It can be useful when you want to create sequence points.
is this actually used
For example, if you try
int success = (printf("Hello "), printf("World!\n"));
You can't really know the success of the first printf
. This is a bad example of using the comma operator in many ways.