Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Comma is a sort of operator which can help form multiple instructions within same instruction.
Learning Objectives
Concept of comma operator.
TC++ #1741
Solution
y=(x=5, ++x),x+=5;
Here y will be initialised with expression immediately following it within the round braces. Within the round braces is also a composite instruction using comma operator. Here x will be first initialised with 5 and then it will be immediately incremented. This will set y to value of 6. Immediately after comma after braces the another instruction will make value of x be added more with 5 and making it 11.
z=((x=5, ++x),x+=5);
Here since the whole RHS expression is in round braces, first inner braces will be evaluated making x as 6 and then 5 more will be added to x in the outer braces. This will make z to be filled with a value of 11.
Notes
Comma operator is useful when single line expression building is important, for e.g. while returning a value from a function. In usual cases multiple instruction lines may be more readable and maintainable as a program.