frank1369blogger's blog

By frank1369blogger, history, 4 years ago, In English

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?

  • Vote: I like it
  • -12
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +23 Vote: I do not like it

<< has a higher priority than ^, so the code is the same as (cout<<2) ^ 3

That will definitely cause an error.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This link contains an operator precedence table, which you can use to compare priorities of operators.

»
4 years ago, # |
  Vote: I like it -11 Vote: I do not like it

Btw I don’t know what does int* mean in c++. Can somebody explain? I searched it on net but I got confused!

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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:

    // Create a pointer to a variable
    int a, b;
    int *c = &a, *d = &a;
    *d = 100; // Changes value of a
    
    c = &b; // c will now point to b instead
    *c = 200; // Changes value of b
    c = NULL; // c is now a null pointer