简体   繁体   English

与 printf(c) 中的解释相同

[英]Is casting the same as interpreting in printf(c)

If i cast a variable type called "a" to another variable type called "b", "b" now is "a" interpreted as the new variable type.如果我将一个名为“a”的变量类型转换为另一个名为“b”的变量类型,“b”现在被“a”解释为新的变量类型。 And if I have one variable type called "a" and I printf it with another variable type, the output will be "a" interpreted as the new variable type.如果我有一个名为“a”的变量类型,并且我将 printf 与另一种变量类型一起使用,则 output 将被解释为新变量类型的“a”。 So is printf making a hidden casting process to do that?那么 printf 是否正在制作一个隐藏的铸造工艺来做到这一点?

If i cast a variable type called "a" to another variable type called "b", "b" now is "a" interpreted as the new variable type.如果我将一个名为“a”的变量类型转换为另一个名为“b”的变量类型,“b”现在被“a”解释为新的变量类型。

No, a cast converts a value to another type;不,强制转换将一个值转换为另一种类型; it does not reinterpret a variable.它不会重新解释变量。 If a is an int with value 3, then (float) a is a float with the same value, 3.如果a是值为 3 的int ,则(float) a是具有相同值 3 的float

And if I have one variable type called "a" and I printf it with another variable type, the output will be "a" interpreted as the new variable type.如果我有一个名为“a”的变量类型,并且我将 printf 与另一种变量类型一起使用,则 output 将被解释为新变量类型的“a”。 So is printf making a hidden casting process to do that?那么 printf 是否正在制作一个隐藏的铸造工艺来做到这一点?

Answering the second question first, no, printf is not doing any casting in this case.首先回答第二个问题,不, printf在这种情况下没有进行任何转换。 Regarding the first question, that is the output that may appear in some cases.关于第一个问题,就是某些情况下可能出现的output。 But the behavior is not defined by the C standard.但该行为并未由 C 标准定义。 Some of the things that may happen include:可能发生的一些事情包括:

  • You pass a variable a of one type but tell printf to expect another type.您传递一个类型的变量a ,但告诉printf期待另一种类型。 printf fetches the bytes of the argument from where you put the bytes of a , but, because it expects a different type, it interprets the bytes as if they were encoded using the rules for the other type, so it reinterprets the bytes of a as the new type. printf从您放置a字节的位置获取参数的字节,但是,因为它需要不同的类型,所以它将字节解释为使用其他类型的规则进行编码,因此它将a的字节重新解释为新类型。
  • You pass a variable a of one type but tell printf to expect another type.您传递一个类型的变量a ,但告诉printf期待另一种类型。 However, the rules for passing these types say to pass them in different places.但是,传递这些类型的规则说要在不同的地方传递它们。 a might be passed in a general processing register, but the other type might be passed in a floating-point register. a可能在通用处理寄存器中传递,但其他类型可能在浮点寄存器中传递。 So printf fetches the bytes from where it expects them to be, but the bytes of a are not there.所以printf从它期望的地方获取字节,但是a的字节不存在。 Instead, printf gets some other bytes that happened to be in the register and converts those for printing.取而代之的是, printf获取其他一些恰好在寄存器中的字节,并将它们转换为打印。
  • The argument passing fails in other ways and corrupts your program.参数传递以其他方式失败并破坏您的程序。
  • The compiler catches the error during compilation and either warns you or refuses to compile your program, depending on settings.编译器在编译期间捕获错误,并根据设置向您发出警告或拒绝编译您的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM