C/C++:long int与long long的区别

本文深入探讨C/C++中的整型数据类型,包括int、long、longint和longlong的区别与联系。揭示了longint在历史背景下的真实含义及其与int的实际等价性,同时解析了longlong作为64位整型的基本类型的角色。

long int
  long int即long,给人的感觉好像是长整型,但实际上,它和int一样,只有32位。cppreference给出的定义是——

int - basic integer type. The keyword int may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it’s guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits.
long - target type will have width of at least 32 bits.

在实际的使用中,long与int几乎没有区别

既然long int与int相同,那么为什么还有long int这种尴尬的类型呢?
  原因是早期的C编译器定义了long int占用4个字节,int占用2个字节,long int是名副其实的长整型。在ANSI C的标准中,对长整型的定义也是long int应该至少和int一样长,而不是long int 一定要比int占用存储字节长。所以,正确的关系应该是——
long≥int≥short long\geq int\geq short
long≥int≥short

新版的C/C++标准兼容了早期的这一设定。

long long
  long long则不同,long long是C++的64位整型的基本类型,“现任”长整型,从C99开始引入这个概念。

原文:https://blog.youkuaiyun.com/CV_Jason/article/details/85244813

0/5 step2/step2.cpp: In function ‘double mySin(double)’: step2/step2.cpp:19:21: error: call of overloaded ‘abs(double&)’ is ambiguous if (abs(next) < 1e-7) break; ^ In file included from /usr/local/include/c++/7.3.0/cstdlib:75:0, from /usr/local/include/c++/7.3.0/ext/string_conversions.h:41, from /usr/local/include/c++/7.3.0/bits/basic_string.h:6349, from /usr/local/include/c++/7.3.0/string:52, from /usr/local/include/c++/7.3.0/bits/locale_classes.h:40, from /usr/local/include/c++/7.3.0/bits/ios_base.h:41, from /usr/local/include/c++/7.3.0/ios:42, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from step2/step2.cpp:1: /usr/include/stdlib.h:837:12: note: candidate: int abs(int) extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur; ^~~ step2/step2.cpp:7:8: note: candidate: double abs(double) double abs(double x) { return x < 0 ? -x : x; } ^~~ In file included from /usr/local/include/c++/7.3.0/cstdlib:77:0, from /usr/local/include/c++/7.3.0/ext/string_conversions.h:41, from /usr/local/include/c++/7.3.0/bits/basic_string.h:6349, from /usr/local/include/c++/7.3.0/string:52, from /usr/local/include/c++/7.3.0/bits/locale_classes.h:40, from /usr/local/include/c++/7.3.0/bits/ios_base.h:41, from /usr/local/include/c++/7.3.0/ios:42, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from step2/step2.cpp:1: /usr/local/include/c++/7.3.0/bits/std_abs.h:56:3: note: candidate: long int std::abs(long int) abs(long __i) { return __builtin_labs(__i); } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:61:3: note: candidate: long long int std::abs(long long int) abs(long long __x) { return __builtin_llabs (__x); } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:70:3: note: candidate: constexpr double std::abs(double) abs(double __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:74:3: note: candidate: constexpr float std::abs(float) abs(float __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:78:3: note: candidate: constexpr long double std::abs(long double) abs(long double __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:84:3: note: candidate: constexpr __int128 std::abs(__int128) abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:102:3: note: candidate: constexpr __float128 std::abs(__float128) abs(__float128 __x) ^~~ step2/step2.cpp: In function ‘double myCos(double)’: step2/step2.cpp:33:21: error: call of overloaded ‘abs(double&)’ is ambiguous if (abs(next) < 1e-7) break; ^ In file included from /usr/local/include/c++/7.3.0/cstdlib:75:0, from /usr/local/include/c++/7.3.0/ext/string_conversions.h:41, from /usr/local/include/c++/7.3.0/bits/basic_string.h:6349, from /usr/local/include/c++/7.3.0/string:52, from /usr/local/include/c++/7.3.0/bits/locale_classes.h:40, from /usr/local/include/c++/7.3.0/bits/ios_base.h:41, from /usr/local/include/c++/7.3.0/ios:42, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from step2/step2.cpp:1: /usr/include/stdlib.h:837:12: note: candidate: int abs(int) extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur; ^~~ step2/step2.cpp:7:8: note: candidate: double abs(double) double abs(double x) { return x < 0 ? -x : x; } ^~~ In file included from /usr/local/include/c++/7.3.0/cstdlib:77:0, from /usr/local/include/c++/7.3.0/ext/string_conversions.h:41, from /usr/local/include/c++/7.3.0/bits/basic_string.h:6349, from /usr/local/include/c++/7.3.0/string:52, from /usr/local/include/c++/7.3.0/bits/locale_classes.h:40, from /usr/local/include/c++/7.3.0/bits/ios_base.h:41, from /usr/local/include/c++/7.3.0/ios:42, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from step2/step2.cpp:1: /usr/local/include/c++/7.3.0/bits/std_abs.h:56:3: note: candidate: long int std::abs(long int) abs(long __i) { return __builtin_labs(__i); } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:61:3: note: candidate: long long int std::abs(long long int) abs(long long __x) { return __builtin_llabs (__x); } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:70:3: note: candidate: constexpr double std::abs(double) abs(double __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:74:3: note: candidate: constexpr float std::abs(float) abs(float __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:78:3: note: candidate: constexpr long double std::abs(long double) abs(long double __x) ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:84:3: note: candidate: constexpr __int128 std::abs(__int128) abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; } ^~~ /usr/local/include/c++/7.3.0/bits/std_abs.h:102:3: note: candidate: constexpr __float128 std::abs(__float128) abs(__float128 __x) ^~~这是问题,错的地方
最新发布
12-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值