[英]How typecasting in c behave when typecast it to longer data type than actual while doing assigenment operation
Here tv_sec is 4 bytes longs but it is typecasting it to 8 bytes variable. 这里tv_sec是4字节长,但是正在将其类型转换为8字节变量。 Variable 't' may have some garbage value in 8 bytes. 变量“ t”可能在8个字节中有一些垃圾值。 How the value will be fill in the remaining 4 bytes. 该值将如何填充剩余的4个字节。
{
long long t;
gettimeofday(&tv,NULL);
t = (long long)(tv.tv_sec) * 1000 ;
// Some Operation
}
From the C standard: 6.3.1.3 (emphesis mine): 根据C标准:6.3.1.3(强调型):
When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged . 将具有整数类型的值转换为_Bool以外的其他整数类型时,如果该值可以由新类型表示,则该值不变 。
Is the type of tv_sec
of integer type? tv_sec
的类型是否为整数类型? Yes. 是。
The type of tv_sec
is time_t
. tv_sec
的类型为time_t
。 However, the underlying type of time_t
in not specified by the C standard. 但是,C标准未指定time_t
in的基础类型。 In POSIX-compliant systems, time_t
represents the number of seconds and is typically implemented as a signed integer
. 在符合POSIX的系统中, time_t
表示秒数,通常以带signed integer
。
A cast causes a conversion. 强制转换会导致转化。 When converting numeric types, if the new type can represent the value in the old type, the result is that value, unchanged. 转换数字类型时,如果新类型可以表示旧类型中的值,则结果将是该值,且保持不变。
Thus, when converting to a wider numeric type, the C implementation produces whatever bits are needed to represent the input value. 因此,当转换为较宽的数字类型时,C实现会产生需要任何位来表示输入值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.