vkditya0997's blog

By vkditya0997, history, 9 years ago, In English

Hello Everyone,

What's the difference in these two macros ?

#define LL long long int 
   and 
typedef long long LL;

and

What's the difference between these ?

inline void myFunction(){}
   and 
void myFunction(){}

Thanks !

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

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by vkditya0997 (previous revision, new revision, compare).

»
9 years ago, # |
  Vote: I like it +18 Vote: I do not like it

For the first one look here. define is low-level preprocessing construct which will blindly replace all LL tokens with long long int regardless of their meaning before compiler even gets a chance to take a look at the code. typedef works on compiler level, it tells compiler to make a new type called LL and make it same as long long, so it won't screw up some other places (see the link for a good example).

For the second one: here. Actually, there is little difference: inline suggests that compiler inline that function wherever it's called instead of pushing arguments to stack and making a jump, can save some time. But it's still only suggestion, compiler is free to do anything it wants: both discard inline modifier and inline function without that modifier. There is some serious difference on linking stage, but that does not matter in competitive programming.

»
9 years ago, # |
  Vote: I like it +32 Vote: I do not like it

Inline? Check this vs this.

»
9 years ago, # |
  Vote: I like it +20 Vote: I do not like it

One clear practical difference is that names introduced by #define cannot be used in other contexts in the program. For example:

#define LL long long int 
void foo(void) {
  int LL = 13; // <- compilation error here
}
  • »
    »
    9 years ago, # ^ |
      Vote: I like it +22 Vote: I do not like it

    Wow, you made my day. Until now I thought that this would not compile with typedef either, but all this C++ namespace and scope stuff can sometimes be ridiculous.

    The code below compiles successfully (at least by g++4-8):

    #include <iostream>
    typedef long long i64;
    
    int main() {
        int i64 = 1;
        std::cout << (::i64)i64 << std::endl;
    }
    
    • »
      »
      »
      9 years ago, # ^ |
      Rev. 2   Vote: I like it +11 Vote: I do not like it

      Standard name lookup rules apply to your example. It compiles for the same reason why the next example compiles — because the inner scope is always looked up first unless the :: operator is used:

      int i = 1;
      int main() {
        long i = 2;
        cout << i << " " << ::i << endl;
      }
      

      And the other way around: trying to define a variable and a type in the same scope with the same name leads to an error.

      typedef long long i64;
      int i64 = 1; // <- error