2

I wanted to know is using

#define ll long long
typedef long long ll;

A good practice, I adopted it few weeks ago after looking at codes of top red coder in codeforces.

Also please explain the difference b/w typedef and #define because everywhere I have studied or spotted these it , they work alike.

Explain #define and typedef use and difference?

Also is it good to use, does the runtime get reduced even if it does get reduced by 0.0000001 second ,please tell.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
Shaurya Uppal
  • 3,410
  • 31
  • 31
  • 3
    What advantage does `ll` have over `long long`? It's hard to understand for anyone looking at your code (which happens much more often than writing code) and saves under a second when typing it, but also wastes time to include whichever header provides this, which also means bringing in another header that you might not have needed otherwise. – chris Apr 21 '17 at 16:24
  • 2
    Neither one of these is a good practice. – Pete Becker Apr 21 '17 at 16:24
  • 2
    Possible duplicate of [Are typedef and #define the same in c?](http://stackoverflow.com/questions/1666353/are-typedef-and-define-the-same-in-c) – Donnie Apr 21 '17 at 16:25
  • Does this just only reduce ones efforts to type long long again and again ? doesn't it have any other advantage? – Shaurya Uppal Apr 21 '17 at 16:26
  • 5
    Don't use code challenge code as good practice. They do this crazy stuff to save keystrokes so they can write code faster that they will never use again. Instead use the full name of things and write clean, readable, maintainable code. – NathanOliver Apr 21 '17 at 16:27
  • 2
    @shauryauppal: Code density. I like being able to pop open a function or even a small class and see the entirety of it on one screen. – Mooing Duck Apr 21 '17 at 16:29
  • @shauryauppal, You could potentially use the type alias as a public API type, letting you change the type without breaking source compatibility. The question looked to me like it was more focused on length. – chris Apr 21 '17 at 17:27
  • One question per question please. – Lightness Races in Orbit Apr 21 '17 at 17:59
  • @Donnie: It would be a duplicate, except that one's not about C++. – Ben Voigt Apr 24 '17 at 02:53

3 Answers3

5

In general, use language constructs rather than pre-processor constructs. The problem with the macro is that every use of ll that follows the definition of that macro will get replaced:

void f() {
    int ll = 3; // whoops, error
}

With the typedef that code is okay.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

Basically one should avoid #define it is not type checked, and the compiler does not see it.

Using a typedef for a basic type is not best practice as well.

However, both have their uses, #define when you want to do something before compilation, such as leaving out debug code in release mode, and typedef which can improve readability when using some longer STL constructs.

didiz
  • 1,069
  • 13
  • 26
0

Fundamentally, the problem with #define has nothing to do with compiler performance.

It has everything to do with clobbering the programming environment in ways that are hard to debug.

contrived example:

#include <iostream>

#define while if

int main()
{
    int i = 10;
    while (i) {
        std::cout << i-- << std::endl;
    }
}

What does this program do?

What would someone who is casually reading the program expect it to do?

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • 3
    Note, however, that this code has an additional problem: it attempts to redefine a keyword, which produces undefined behavior. – Pete Becker Apr 21 '17 at 16:34