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 - Assignment, equality comparison problem
Find the output of the program given below. Assume all relevant header files are included
int main( )
{
int a,b,c=101,d=102;
a=(c=d);
cout<<a<<endl;
b=(c==d);
cout<<b<<endl;
return 0 ;
}
Solution
TC++ #2314
Run Output
102
1
Notes
int a,b,c=101,d=102;
Here a and b do not have initial values and c and d have values 101 and 102 respectively.
a=(c=d);
Here left hand side inside will be evaluated first where the value of d is assigned to c. So c will leave its original value and will be assigned the value of d which is 102. So from left hand side c will contain 102. Finally with the last assignment operation a will contain the new value of c which will be 102.
cout<<a<<endl;
This will print 102 as explained above.
b=(c==d);
Here the operator inside bracket on the right hand side is the comparison operator for equality check. Since c has already be assigned 102 in the previous operation, so it will become equal to d which was originally set for 102. Which means that comparison operation will return true status. In c++ if comparison evaluates to true, it is stored as 1 if equated to an integer storage. So b will be 1.
cout<<b<<endl;
This will print 1 as explained above.
Common Errors
The modification of value of cΒ to value of d in first expression is often missed by students.
The watch over difference between = and == operator is essential for this kind of programs.