转换为更大的整数类型
Converting to a larger integer type
我知道这不是在 Whosebug 上提出的最佳问题,但我正在寻找用于描述转换为更大的数字类型以处理溢出时的术语。一个基本的例子是:
# int is 32 here
int a = 2147000000;
int b = 10000000;
printf ("The sum of a and b is: %ld", (long)a+b);
'converting to a wider type' 的术语是什么,是显式还是隐式?
来自标准(草案 N1570):
6.5.4 Cast operators .... Preceding an expression by a parenthesized type name converts the value of the
expression to the named type. This construction is called a cast.
因此标准使用术语:convert
因此,使用强制转换,您可以将 int
转换为 long
标准也经常使用“转换”,比如从int
到long
的转换
What is the term for 'converting to a wider type', whether done explicitly or implicitly?
我建议加宽 - 实际上就是 OP 在问题中使用的内容。
C 规范使用 wide, wider, widest 来描述从某种类型到更高等级和范围的一种,如 int
到 long long
。 (狭窄,... 走另一条路)。这也适用于 float, double, long double
.
int
和 long
可能具有相同的宽度,但 (long)a+b
不能肯定地防止溢出。使用 long long
或 intmax_t
更有可能 提供扩展范围。
// printf ("The sum of a and b is: %ld", (long)a+b);
printf ("The sum of a and b is: %jd", (intmax_t)a+b);
我知道这不是在 Whosebug 上提出的最佳问题,但我正在寻找用于描述转换为更大的数字类型以处理溢出时的术语。一个基本的例子是:
# int is 32 here
int a = 2147000000;
int b = 10000000;
printf ("The sum of a and b is: %ld", (long)a+b);
'converting to a wider type' 的术语是什么,是显式还是隐式?
来自标准(草案 N1570):
6.5.4 Cast operators .... Preceding an expression by a parenthesized type name converts the value of the expression to the named type. This construction is called a cast.
因此标准使用术语:convert
因此,使用强制转换,您可以将 int
转换为 long
标准也经常使用“转换”,比如从int
到long
What is the term for 'converting to a wider type', whether done explicitly or implicitly?
我建议加宽 - 实际上就是 OP 在问题中使用的内容。
C 规范使用 wide, wider, widest 来描述从某种类型到更高等级和范围的一种,如 int
到 long long
。 (狭窄,... 走另一条路)。这也适用于 float, double, long double
.
int
和 long
可能具有相同的宽度,但 (long)a+b
不能肯定地防止溢出。使用 long long
或 intmax_t
更有可能 提供扩展范围。
// printf ("The sum of a and b is: %ld", (long)a+b);
printf ("The sum of a and b is: %jd", (intmax_t)a+b);