Using result of comma operator as l-value is not valid in C. But in C++, result of comma operator can be used as l-value if the right operand of the comma operator is l-value.
For example, if we compile the following program as a C++ program, then it works and prints b = 30. And if we compile the same program as C program, then it gives warning/error in compilation (Warning in Dev C++ and error in Code Blocks).
#include<stdio.h> int main() { int a = 10, b = 20; (a, b) = 30; // Since b is l-value, this statement is valid in C++, but not in C. printf("b = %d", b); getchar(); return 0; } |
C++ Output:
b = 30
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- lvalue and rvalue in C language
- Comma operator should be used carefully
- A comma operator question
- How can we use Comma operator in place of curly braces?
- Overloading the Comma Operator
- Comma in C and C++
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- Default Assignment Operator and References
- Operator Precedence and Associativity in C
- Inserting elements in std::map (insert, emplace and operator [])
- Overloading New and Delete operator in c++
- Difference between Relational operator(==) and std::string::compare() in C++
- Increment (++) and Decrement (--) operator overloading in C++
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++

