#include <iostream>
using namespace std;
#define ll long long
int main() {
int a = 5;
ll maxi = 1;
maxi = max(maxi, maxi * ll(a));
cout<<maxi<<endl;
return 0;
}
Why does this code throw an error? I don't understand what's wrong with #define ll long long .
Remember that
#defineperforms a textual substitution. You end up with this:Which is invalid, since the type name for a functional cast can't, roughly speaking, contain spaces at the top level. So,
unsigned int(a),long double(a), etc, are all invalid for this the same reason.The solution is either to use
usinginstead of#define:or to do
(ll)a, since in this case the spaces are allowed.But if I were you, I would get rid of
lland use(long long)a, sincellis a rather non-descriptive name.Note that
#define ll long longis a misuse of macros, and is bad for many reasons:It's confusing. From seeing
ll maxi = 1;, a reasonable person would expectllto be a type andll(a)to work. And it would work if it wasn't a macro.The short name can conflict with things. If you put this
#definein a header, and then include another header that uses the wordllfor any purpose whatsoever, it will break.To avoid that, macros should have long scary ALL_CAPS names.
Macros should be the last nuclear option, used when everything else fails, not just when you don't feel like typing some extra letters.
It's something you see on competitive programming sites, but if you attempted this at an actual job, it would fail any sane code review.