-3

I was trying to define ll as an alias for long long. However, this didn't compile and threw an error.

I am using VS Code, on a Windows machine. I'm also using gcc version 8.2.0.

This is the code -

#include <bits/stdc++.h>

using namespace std;

#define ll long long int;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;

    cin >> t;

    return 0;
}

On compiling this, I got this error -

test.cpp: In function 'int main()':
test.cpp:5:22: error: declaration does not declare anything [-fpermissive]
 #define ll long long int;
                      ^~~
test.cpp:12:5: note: in expansion of macro 'll'
     ll t;
     ^~
test.cpp:12:8: error: 't' was not declared in this scope
     ll t;
        ^
test.cpp:12:8: note: suggested alternative: 'tm'
     ll t;

The weird thing is that this exact code works on other machines. Can someone please explain this to me?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
jindan
  • 878
  • 1
  • 6
  • 9

1 Answers1

6

There are no semicolons after preprocessor directives.

So this:

#define ll long long int;

means ll is literally long long int;.

Then your declaration:

ll t;

is really:

long long int; t;

Which is the same as:

long long int;
t;

Hopefully now you can see why your compiler hates it.


As an aside, I realise you're doing "competitive programming [sic]" and that it is hip in that field to make everything short and unreadable, but macros like these are really to be avoided if you want to write anything approaching decent code. Similarly, do not include implementation headers.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you for the advice. I am just starting out on competitive programming and I saw a bunch of other people using this to shorten their code. I didn't realise that this is ill-advised. – jindan Aug 06 '19 at 14:04