Detailed Print – CSKC
Sign In

Search

siteicon
Solved Problem#CPP#5573 Back

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

		 

Click to open popup

Run Output

1

Solved Problem Understanding

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.

 

Notes


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