+ 2

Can any one explain how this output comes?

What is the output of this code? int i=1, j=2, k=3; int w; w=(i,j,k); cout<< w; int r; {r=i,j,k}; cout << r; int q; q= i, j, k; cout << q; //Output 311

23rd Oct 2019, 5:40 AM
Malem Yengkhom
Malem Yengkhom - avatar
21 Answers
+ 9
🇮🇳Omkar🕉 My understanding regarding variable declaration and definition in C++ are as follows: A variable declaration involves declaring the type and the symbol name for a variable. A variable definition involves defining the storage location for that variable which might be defined in another file or when the variable is declared. Therefore, the first line of the code below could be considered both declared and defined. It declares the type and symbol name and defines the variable is global. int i; int main() { i = 42; } On the other hand, as pointed out by ~ swim ~, the line below only declares the variable which is defined elsewhere: extern int i; The article linked below explains these differences quite well: https://www.cprogramming.com/declare_vs_define.html
23rd Oct 2019, 8:53 AM
David Carroll
David Carroll - avatar
+ 7
According to Wikipedia https://en.m.wikipedia.org/wiki/Comma_operator The comma operator evaluates it's first operand and discards its result and returns result of second operand. So the expression (i, j, k) will return only k other operands like i and j will be discarded. therefore w is initialized with k (3) So cout<<w; gives you 3. 🤔Most important thing to remember here is that comma operator has least precedence in all operators! So in order to make sure that right hand side gets evaluated first they are using perantheses (). https://en.cppreference.com/w/c/language/operator_precedence next r=i,j,k; As stated earlier comma has least precedence. So = operates first and r gets initialized with i (1) printing it you get 1. The same logic as above for next q =i, j, k; q = i; is evaluated first due to operator precedence. Printing q you get 1 Hence 311 Happy learning. Wish you success!
23rd Oct 2019, 6:54 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 7
~ swim ~ sometimes does words confuse me too 😂😂😂
23rd Oct 2019, 7:59 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 6
In w = (i,j,k) we assigned each value to w so in this case last value will be assigned to w so value is 3 In { r = i, j, k } We assigned the value of i to r within the block so r will be 1 In q = i, j, k we assigned the value of i to q without blocks so q will be 1 curly braces doesn't matter to assign value. It just confuse you.
23rd Oct 2019, 6:49 AM
A͢J
A͢J - avatar
+ 6
A J In simplest words. int x; //declaration int a =5; // initialization int foo; foo = 5; //assignment. Declaration will introduce an identifier, int x; will make x be variable of type x void bar(); will make bar a function identifier //will introduce prototype. class C; will make C a class. Etc. It's basically introduction of identifier. initialization - assigning object, variable it's initial value. int x =5; Assignment - assigning a value after declaration. Assignment can happen any timeas long as identifier is in scope. But initialization is done when identifier is introduced. Initialization can be called combination of assignments and declaration. *Anyone correct if wrong.
23rd Oct 2019, 7:52 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 6
~ swim ~ now definition is fourth. Now one more confusion 😁
23rd Oct 2019, 8:00 AM
A͢J
A͢J - avatar
+ 5
A J "In w, (i, j, k) we assigned each value to w so in case last valye will be assigned to w so value is 3 " - AJ r is not assigned all values, it gets initialized with value returned by expression within parantheses on right hand side. Next *Block don't make any change here. It's precedence that causes r to get initialized by i, before operation of comma. Same for last statement. Thank you. Keep helping 🤗
23rd Oct 2019, 7:01 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 5
Thanks 🇮🇳Omkar🕉 and ~ swim ~ for explanation. I need to learn more on c and c++. But assigned by i and initialised by i both are same. in both case others value will change on change of i
23rd Oct 2019, 7:06 AM
A͢J
A͢J - avatar
+ 5
~ swim ~ 😢 😁thank you friend. Still I don't think it's definition. 😅 Because it declares x as an integer. Let me give an example, class MyClass; //declare class SwimsClass{ int answers; }; // I think of SwimsClass as definition 🤔 So in int x; we are not explaining(defining)what a int be like. just telling that it'll be int I have mostly seen declarations , definition words in case of functions. declaration - just prototype. definition - explain working , body edit: int getAge(); //decalre int getAge() //definition, includes body. { return age; } *Again free for correcting. 😇
23rd Oct 2019, 8:02 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 4
~ swim ~ initialization in block scope don't make any change 🤔(only declaration will do) Am I right?
23rd Oct 2019, 7:05 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 4
Initialize, declaration and assignment 🇮🇳Omkar🕉 ~ swim ~ Can you tell the difference between them. It's little bit confusing.
23rd Oct 2019, 7:17 AM
A͢J
A͢J - avatar
+ 4
Malem Yengkhom It's related to this question so we can.
23rd Oct 2019, 7:21 AM
A͢J
A͢J - avatar
+ 4
~ swim ~ Reference site 📝call declaring/defining primitive type variables as declaration http://www.cplusplus.com/doc/tutorial/variables/ (Section :Declaration of variables ) Quoting a sentence " C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use. " Anyways , That's lot of confusion. We can call it whatever we like. Only thing that matters is code should work😁👌 Thanks for all corrections and your time 🤝
23rd Oct 2019, 8:38 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
But this kind of topic is not discuss in sololearn here
23rd Oct 2019, 7:15 AM
Malem Yengkhom
Malem Yengkhom - avatar
24th Oct 2019, 10:27 AM
A͢J
A͢J - avatar