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!
Problem Statement - Finding Valid Multiple value assignments
Which of the following statements is/are valid and why.
int x;
(i) x=1,024;
(ii) x=(1,024);
Solution
TC++ #5573
Run Output
1
Both the statements are valid.
int x; x=1,024; cout<<x<<endl;
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 024 (Octal 24) will just act as stray expressions and will not have any impact.
x=(1,024); cout<<x<<endl
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. Since last one is 024 which is a way to write Octal values in c++ so decimal version of Octal 24 which is 20 will be printed.