Francisco
So it's not as powerful as -O2 or -O3
Francisco
Yep, just had a mental breakdown e.e
Francisco
Exams get me exhausted
Rashi
*Today's question* Q)What will be output of the following question #include <stdio.h> int main() { int i; if(i=(2,1,0)) printf("welcome "); else printf("Hello "); printf("%d\n",i); } a)Hello 3 b)welcome 0 c)Hello 0 d)welcome 3 Hey everyone . I have a doubt in this question . I know precedence of paranthesis is greater than that of equality ,and associativity of paranthesis is from left to right .. that's y 0 gets stored in i. But since 0 is simply assigned in i , how does this makes if condition false?
Pavel
wtf is if(i=(2,1,0))?
Rashi
wtf is if(i=(2,1,0))?
If condition ...
Pavel
If condition ...
can you explain what exactly happens there, why there are 2,1,0?
Pavel
because I don't remember ever seeing such syntax
Rashi
i=0 is false, because it returns 0, and 0 is the only integral that casts to false
i= 0 means assignment of 0 in a variable named i. Is int i=0; if i Same as int i=0;
Rashi
can you explain what exactly happens there, why there are 2,1,0?
i=(2,1,0) evaluates to i= 0 bcoz associativity of paranthesis is from left to right . There is nothing wrong in this syntax
Pavel
i= 0 means assignment of 0 in a variable named i. Is int i=0; if i Same as int i=0;
yep but you can do int a,b,c; a = b = c = 5; it will execute c = 5, the result will be 5 and it will be assigned to b, and the result of this will again be 5 and then it will be assigned to a. in your case 0 is assigned and result (again 0) is casted into bool
Pavel
Comma operator
so it's like "execute and forget about it"? in this case 2 and 1 will be just skipped?
Anonymous
So what is if(i=(1,2,0)) ?
Rashi
Do you send these type of questions daily?
No...I get these questions in my uni's coding club group ... I was having a doubt today
Itsunknown4u
The answer is C
Rashi
The answer is C
I know that pretty well. I was asking for the explanation I guess
Nyuke
No...I get these questions in my uni's coding club group ... I was having a doubt today
Can you suggest me some sites which will upload some questions about cs or c/c++ daily? Or it doesn't exist?
Pavel
Thanks
Anonymous
If Statement
I know that it's an if statement but what does it evaluates to?
Francisco
The comma operator returns the last operand. It's been used for a lot of (doubtful) things. It even led to some library implementors to think about it's overloading (yes, you can overload the comma operator)
Francisco
Nope
Pavel
I know that it's an if statement but what does it evaluates to?
I tried to explain it earlier So (2,1,0) returns zero i=0 returns zero if(0) evaluates as false And you got Hello from that branch
Pavel
How does it work
Which step?
Pavel
3 params in one parantheis
Oh, wait sry, I thought it's you asked the initial question :)
Pavel
https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator
Beer WRLD
3 params in one parantheis
Its comma operator... Google it
Beer WRLD
Comma can be used as an operator too ...
Itsunknown4u
I know that pretty well. I was asking for the explanation I guess
If statement gets executed only when it gets its condition true(i.e 1 ) so by initialisation i =1, the if statement will be executed ....if u assign any other value then non zero value it will get executed ...but when u assign 0 ...it gets false
Siddharth Sahoo
https://www.spoj.com/problems/BSEARCH1/ Can somebody help me with uploading this problem's solution in spoj. I am fairly new in competitive programming .In this binary search problem my code is running fine, outside in my local compiler but I am unable to know why my code is not accepted by the spoj. Help me out. Here is my code: #include <bits/stdc++.h> using namespace std; int arr[100],numberOfElemenets; int BinarySearch(int x){ int mid,left=0,right=numberOfElemenets; while(left<=right){ mid=(left+right)/2; if(arr[mid]==x) return mid; else if (arr[mid]>x) right = mid-1; else left = mid +1; } return -1; } int main() { int n; cout<<"Enter the number of elements: "; cin >> n; for(int i=0 ; i<n ; ++i){ cin>>arr[i]; } for(int i=0 ; i<n ; ++i){ cout<<arr[i]<<" "; } cout<< endl; cout<<"The elements is present at :"<<BinarySearch(2)<<endl; cout<<"The elements is present at :"<<BinarySearch(4)<<endl; cout<<"The elements is present at :"<<BinarySearch(8)<<endl; }
Anonymous
Its comma operator... Google it
how does value of m is calculated
Anonymous
int n = 1; int m = (++n, std::cout << "n = " << n << '\n', ++n, 2*n);
Beer WRLD
👍
Anonymous
++n is first evaluate although its result is discarded
Anonymous
so n becomes 2
Anonymous
what after that
Beer WRLD
what after that
Last one is assigned dats what i read
Anonymous
how is m getting calculated
Anonymous
what does std::cout gives
Anonymous
?
Beer WRLD
Left to right executed but last one is assigned
Beer WRLD
how is m getting calculated
Value of m will be 2
Anonymous
no
Anonymous
it should be
Anonymous
6
Beer WRLD
https://www.google.com/amp/s/www.geeksforgeeks.org/comna-in-c-and-c/amp/
Anonymous
bcz
Anonymous
In int m = (++n, std::cout << "n = " << n << '\n', ++n, 2*n);
Beer WRLD
bcz
Yeah
Anonymous
So, im right?
Beer WRLD
Left to right execution n also ++n there
Beer WRLD
Beer WRLD
Have to confirm again 😅 ...i just came through it rn
Anonymous
Also wanted to ask about what will be the result of n=1; cout«++n«n+4«n++;
Anonymous
7 6 1
Anonymous
is above thing right
Beer WRLD
7 6 1
It shud be in otherway ig
Anonymous
Anonymous
is my ans correct if printf is used instead of cout
Beer WRLD
is my ans correct if printf is used instead of cout
Not sure about printf ...cz i used cout always... I have just passed my higher secondary so i am upto dat level😅
Beer WRLD
This would help
Anonymous
so Im right
Anonymous
is it
Anonymous
right to left in case of printf