开发者

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

开发者 https://www.devze.com 2023-01-08 23:15 出处:网络
This is probably just an inconsistency of notation at cplus开发者_StackOverflowplus.com, but is there a difference between \"long int\" and \"long\" types in C++?cplusplus.com says that abs takes inpu

This is probably just an inconsistency of notation at cplus开发者_StackOverflowplus.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?


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.


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.


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); }


long and long int are equivalent and interchangeable.

0

精彩评论

暂无评论...
验证码 换一张
取 消