EvilZone
Programming and Scripting => C - C++ => : Psycho_Coder July 22, 2013, 07:27:10 PM
-
In C and C++, comma (,) can be used in two ways:
Syntax
expression:
assignment-expression
expression , assignment-expression
The left operand of the sequential-evaluation operator is evaluated as a void expression. The result of the operation has the same value and type as the right operand. Each operand can be of any type. The sequential-evaluation operator does not perform type conversions between its operands, and it does not yield an l-value. There is a sequence point after the first operand, which means all side effects from the evaluation of the left operand are completed before beginning evaluation of the right operand.
The sequential-evaluation operator is typically used to evaluate two or more expressions in contexts where only one expression is allowed.
Commas can be used as separators in some contexts. However, you must be careful not to confuse the use of the comma as a separator with its use as an operator; the two uses are completely different.
1) Comma as an operator:
The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.
A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.
/* comma as an operator */
int i = (5, 10); /* 10 is assigned to i*/
int j = (func1(), func2()); /* func1() is evaluated first followed by func2().The returned value of func2() is assigned to j */
2) Comma as a separator:
Comma also acts as a separator when used with function calls and definitions, function like macros, variable declarations, enum declarations, and similar constructs.
/* comma as a separator */
int a = 1, b = 2;
void func(x, y);
The use of comma as a separator should not be confused with the use as an operator. For example, in below statement, func1() and func2() can be called in any order.
/* Comma acts as a separator here and doesn't enforce any sequence. Therefore, either f1() or f2() can be called first */
void func(func1(), func2());
Have a look at this :- http://stackoverflow.com/questions/2087026/effect-of-using-a-comma-instead-of-a-semi-colon-in-c-and-c (http://stackoverflow.com/questions/2087026/effect-of-using-a-comma-instead-of-a-semi-colon-in-c-and-c)
See the programs below to check your understanding of comma in C.
#include<stdio.h>
int main()
{
int x = 10;
int y = 15;
printf("%d", (x, y));
getchar();
return 0;
}
#include<stdio.h>
int main()
{
int x = 10, y;
//Equavalent to y = x++
y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++);
// Note that last expression is evaluated
// but side effect is not updated to y
printf("y = %d\n", y);
printf("x = %d\n", x);
getchar();
return 0;
}
References:
http://en.wikipedia.org/wiki/Comma_operator (http://en.wikipedia.org/wiki/Comma_operator)
http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx (http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx)
-
Nice bit of theoretical programming. Would like to see some more of these articles. Concepts like tail call optimization, operator and function overloading, string interpolation, etc.
-
Good topic and rough overview. Have a cookie.
A quick note on one of the pieces of example code...
...
#include<stdio.h>
int main()
{
int x = 10, y;
//Equavalent to y = x++
y = (x++, printf("x = %d\n", x), ++x, printf("x = %d\n", x), x++);
// Note that last expression is evaluated
// but side effect is not updated to y
printf("y = %d\n", y);
printf("x = %d\n", x);
getchar();
return 0;
}
...
Actually, that line is equivalent to 'y = (x + 2)'. There are 3 increments of x which are performed, only the second one takes place before the assignment. The first 'x++' is post-increment, which reads the value of 'x' first and then increments it. The second, '++x', is pre-increment, which increments first and reads the value afterward. The third, 'x++', being post-increment, reads the value(which is what is assigned to 'y') and then increments the variable. The result is that the 2nd printing of 'x' and the only printing of 'y' should be equal whilst the final printing of 'x' is greater.
The output should be something like:
x = 11
x = 12
y = 12
x = 13
(NOTE: I didn't actually run this so I could be wrong...but I sincerely doubt it.)
-
And may I ask why you would really want to use the comma operator a lot? Just because you can do it doesn't mean you should. In my opinion it takes too long to read and understand what you're doing if you do complicated things with it and so really I would say you should instead just split it up into multiple lines. (Variable definitions are another thing, I'm talking about int I = (1,2); that just takes a ton of time to figure out especially if you're not even used to it and that's just the simple version)
-
And may I ask why you would really want to use the comma operator a lot? Just because you can do it doesn't mean you should. In my opinion it takes too long to read and understand what you're doing if you do complicated things with it and so really I would say you should instead just split it up into multiple lines. (Variable definitions are another thing, I'm talking about int I = (1,2); that just takes a ton of time to figure out especially if you're not even used to it and that's just the simple version)
Its not about what can be done with this. Its about knowing the things and the concepts as it is applicable.
-
Sometimes I do find it useful. I'll agree that it shouldn't be used everywhere, just because it's a known option. However, there are times when its use can actually appear to simplify things.
From the CIDR check (http://evilzone.org/c-c/cidr-check/) code that I posted:
if (argc < 2) return (fprintf(stderr, "Must supply CIDR argument.\n"), 1);
In this case, the function returns '1' after printing an error. I'll grant you that it may not be entirely clear to everyone, but for those that are familiar with it, it simplifies things greatly. The same could be said for structs & unions, I think. Such things may not be clear to all, particularly those still learning, but it definitely simplifies the code for those already familiar with their use.
-
Sometimes I do find it useful. I'll agree that it shouldn't be used everywhere, just because it's a known option. However, there are times when its use can actually appear to simplify things.
From the CIDR check (http://evilzone.org/c-c/cidr-check/) code that I posted:
if (argc < 2) return (fprintf(stderr, "Must supply CIDR argument.\n"), 1);
In this case, the function returns '1' after printing an error. I'll grant you that it may not be entirely clear to everyone, but for those that are familiar with it, it simplifies things greatly. The same could be said for structs & unions, I think. Such things may not be clear to all, particularly those still learning, but it definitely simplifies the code for those already familiar with their use.
Absolutely :)