Multiple value assignment attempts – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#5571    siteicon   siteicon  

Multiple value assignment attempts

Here we demonstrate the treatment of precedence between comma, brackets and assignment operator in a multiple value assignment scenario.

Learning Objectives

  • Learning assignments in relation with operator precedence with respect to comma and parentheses.

Source Code

TC++ #5571


Source Code

Run Output


Code Understanding

//int x=1,2,3 //Will give error
This assignment is not valid as = operator has higher precedence than , comma operator. So 1 would be assigned to x but then compiler will try to declare 2 and 3 as integer variable. This is not possible as a variable name can not begin with a number.

int x=(1,2,3);
This assignment will work as parentheses will be evaluated first. Then assignment will happen by evaluating each term from left to right. Since the expressions inside brackets are separated by commas the last one will be assigned to x.

As per above logic last value has been assigned to x , so this will print 3.

x = 1,2,3;
This one is a case where declaration has already been done previously. Here we are only assigning values. Since assignment will have higher precedence from comma so x will be assigned as 1 here. Value 2 and 3 will just act as stray expressions and will not have any impact.

As per logic given above this will print 1.


  • The evaluation of such confusing expressions must be done purely on the basis of operator precedence rules.

Suggested Filename(s): assignmu.cpp, assign-multiple.cpp


CSKC| Created: 19-Dec-2018 | Updated: 19-Dec-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues