This forum is in archive mode. You will not be able to post new content.

Author Topic: Comma : Sequential-Evaluation Operator  (Read 1874 times)

0 Members and 2 Guests are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Comma : Sequential-Evaluation Operator
« on: July 22, 2013, 07:27:10 PM »

In C and C++, comma (,) can be used in two ways:


Syntax


Code: [Select]
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.


Code: [Select]
/* 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.


Code: [Select]
/* 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.


Code: [Select]
/* 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


See the programs below to check your understanding of comma in C.


Code: [Select]
#include<stdio.h>
int main()
{
   int x = 10;
   int y = 15;
 
   printf("%d", (x, y));
   getchar();
   return 0;
}


Code: [Select]
#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://msdn.microsoft.com/en-us/library/zs06xbxh.aspx
« Last Edit: July 22, 2013, 07:29:11 PM by Psycho_Coder »
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Comma : Sequential-Evaluation Operator
« Reply #1 on: July 22, 2013, 07:54:24 PM »
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.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Comma : Sequential-Evaluation Operator
« Reply #2 on: July 23, 2013, 09:09:57 AM »
Good topic and rough overview.  Have a cookie.

A quick note on one of the pieces of example code...

...
Code: [Select]
#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:
Code: [Select]
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.)
-Xires

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Comma : Sequential-Evaluation Operator
« Reply #3 on: July 23, 2013, 02:33:35 PM »
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)
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: Comma : Sequential-Evaluation Operator
« Reply #4 on: July 23, 2013, 04:02:06 PM »
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.
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Comma : Sequential-Evaluation Operator
« Reply #5 on: July 23, 2013, 04:02:46 PM »
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 code that I posted:
Code: (c) [Select]
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.
-Xires

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: Comma : Sequential-Evaluation Operator
« Reply #6 on: July 23, 2013, 05:14:52 PM »
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 code that I posted:
Code: (c) [Select]
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 :)
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.