0

Recently I came across the below given code on a competetive programming website,

   #include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define d double
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

signed main()
{ return 0; //Omitted the rest of the code
}
[Full Code][1]

https://www.codechef.com/viewsolution/22121098

I would like to know what's the difference between this code and a regular C++ code with int main() in terms of efficiency and performance in terms of CPU speed, The problem sets are often huge.

  • Possible duplicate of [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) –  Dec 29 '18 at 19:54
  • 8
    You are not allowed to redefine keywords. `main` must return `int` (`signed` is the same type as `int`). If you don't follow these rules your program is ill-formed at best and has undefined behavior at worse. Don't learn programming from these sites. –  Dec 29 '18 at 19:54
  • 3
    The header `bits/stdc++.h` is compiler specific and not part of the *standard* C++ language. Include only the header files you need. – Thomas Matthews Dec 29 '18 at 19:58
  • Don't use macros to make abbreviations. It actually adds time to the build process because the preprocessor has to substitute the definitions for the macro names. Also makes coding difficult to read because the reader will need to find the definition macro. If you want to speed up your typing, take a keyboarding class. See also [The International Obfuscated C Code Contest](https://www.ioccc.org/). Obfuscated code is not a substitute for encryption or protection from hacking or duplication. – Thomas Matthews Dec 29 '18 at 20:02
  • 3
    And just to clarify, `signed main()` and `int main()` are absolutely identical, the two type names alias the same type. It is extremely unusual to write `int` as `signed` though and would confuse most readers. –  Dec 29 '18 at 20:04
  • 2
    In the industry, maintainable and readable code is preferred to compact code that is difficult to maintain. Most of the lifetime of code is in maintenance: fixing defects and adding features. Code that is difficult to maintain means more time and more money in the maintenance phase; which is a Bad Thing. Rewriting Bad Formed Code may be faster and easier than trying to understand it; which is wasting effort due to the rewrite. – Thomas Matthews Dec 29 '18 at 20:05
  • 4
    Ugh those websites. Who can make the most broken, horrible code the fastest - yay! – Lightness Races in Orbit Dec 29 '18 at 20:06
  • @user10605163 yes, you are right I shouldn't be learning from these sites but the person whos wrote this is fairly advanced in nature with his ratings in Divison 1, so wanted to know the reason. – Akhilesh Pandey Dec 29 '18 at 20:06
  • 5
    In terms of speed, the `main` function is executed once. The return type is at the end of `main` and usually quick to return an integer. As far as build time goes, the difference is negligible, as your talking 3 letters versus 6. Same goes for typing. – Thomas Matthews Dec 29 '18 at 20:07
  • @ThomasMatthews thanks for the wonderful advice, and this code is not written by me but a fairly advanced competetive programmer, I will pay heed to your words in future. – Akhilesh Pandey Dec 29 '18 at 20:08
  • 6
    Just because someone is an advanced competitive programmer in "division 1" doesn't mean they're actually any good at programming. Frankly I'd suspect the opposite. – Lightness Races in Orbit Dec 29 '18 at 20:09
  • 1
    If you have the correct solution to a problem, stuff like this shouldn't matter. If you don't, your program won't run in time no matter how optimized it is. You're much better off spending your time to improve on your problem-solving skills than to try to make every single bit of your program as fast as possible at the cost of not being able to write good maintainable code. – eesiraed Dec 29 '18 at 20:09
  • 1
    @LightnessRacesinOrbit Couldn't agree more –  Dec 29 '18 at 20:10

2 Answers2

5

signed main() is equivalent to int main() unless - like in the example - you have a macro which defines int as long long. main() has to return an int but the macro #define int long long means using the syntax int main() in this case will not compile with an error stating ::main must return int. Hence, signed main().

William Miller
  • 9,839
  • 3
  • 25
  • 46
5

signed is signed int which is also int.

So signed main() is int main().

The author either wanted to appear clever, or was trying to be clever and failed.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055