14

recently I get to know about a special function in c++ : __gcd(A,B). this will return the greatest common divisor of A and B.

#include<iostream>
#include<algorithm>
using namespace std;

main()
{
cout<<__gcd(10,40); //op: 10
}

is there any special reason for starting function definition with 2 underscores?

It could be as simple as gcd(A,B) like other STL functions.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • *like other built-in functions* which ones are you referring to? – Borgleader Jun 17 '15 at 17:39
  • 6
    I don't believe it was intended for the public to use. – AndyG Jun 17 '15 at 17:39
  • 1
    @Borgleader like sort() ,qsort() ,swap( ) etc – GorvGoyl Jun 17 '15 at 17:40
  • 6
    @JerryGoyal all those are standard library functions (as in, they are required to be there by the C++ standard), __gcd is gcc specific and therefore not portable. [Related question](http://stackoverflow.com/questions/13443463/built-in-functions-in-c) – Borgleader Jun 17 '15 at 17:41

2 Answers2

16

Names starting with two underscores are reserved for the implementation, which means that you are not allowed to define such names in your code, and there are no standard guarantees what those names mean if they do exist. However, a vendor might choose to document some such names, in which case you can use them with the product for which the vendor documents them.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
5

In C++17 there are standard library functions for GCD and LCM.

#include <iostream>
#include <numeric>

int main ()
{
    int a, b;
    std::cin >> a >> b;
    std::cout << std::gcd(a,b) << '\n';
    return (0);
}
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
Kuji
  • 53
  • 1
  • 5