flies
flies

Reputation: 2155

difference between "long" and "long int", abs & labs

This is probably just an inconsistency of notation at cplusplus.com, but is there a difference between "long int" and "long" types in C++? cplusplus.com says that abs takes inputs of types "int" and "long", whereas labs uses "long int". I assume that this is basically a typo. If so, then is the only difference between abs and labs that labs is guaranteed to return a long?

Upvotes: 6

Views: 6197

Answers (4)

Philipp
Philipp

Reputation: 49850

long int is the same type as long. abs and labs are from C where there is no function overloading. long abs(long) is the same as long labs(long) in C++. For example, GCC has

inline long abs(long __i) { return labs(__i); }

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942478

They are the same. Similar to "unsigned" and "unsigned int". Yes, in C++ there's an overload for abs() that takes a long argument. labs() is necessary for C programmers, they can only use the abs() function that takes an int. The C language doesn't support function overloading.

Upvotes: 1

jpalecek
jpalecek

Reputation: 47770

There is no difference between long and long int.

The reason we have abs(long) and labs(long) (while both are equivalent) is that labs() is a remnant of the C library. C doesn't have function overloading, so function abs() can only take one type (int) and the long one has to be called differently, hence labs.

Upvotes: 17

Bill
Bill

Reputation: 14695

long and long int are equivalent and interchangeable.

Upvotes: 0

Related Questions