This code gives me compilation error:
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << 2 ^ 3;
return 0;
}
But when instead of 2 ^ 3 I write (2 ^ 3) it becomes OK. Does anybody know why?
<<
has a higher priority than^
, so the code is the same as(cout<<2) ^ 3
That will definitely cause an error.
thanks
This link contains an operator precedence table, which you can use to compare priorities of operators.
Btw I don’t know what does int* mean in c++. Can somebody explain? I searched it on net but I got confused!
The basic answer is, int stores an integer whereas an int* stores an address where the value is an integer.
For example: int a = 5; int* b = &a; cout<<a;// 5 cout<<(&a);//0x12345(outputs b) cout<<b;//0x12345(outputs an adress) cout<<(*b);//5
int * x;
means that you are declaring a variable which will contain a pointer to integer, rather than the actual integer value itself. This is a big topic, which is usually quite confusing for a lot of people at the beginning. You may want to look up pointers and references in C++. I could also give you a short code which you can play with to understand the basic principles: