简体   繁体   English

使用 C 中的格式说明符打印

[英]Printing with format specifiers in C

In a class quiz, I was asked to write the output of the below code snippet.在 class 测验中,我被要求编写以下代码片段的 output。

x = 1.234;
printf("x=%2d");

The variable x contains point values so I assumed it to be a float/double type.变量x包含点值,所以我假设它是浮点/双精度类型。

In the paper I answered that this code would just simply print the statement within quotes (as x=%2d ), as in print function it prints whatever within the " " as it is.在论文中,我回答说这段代码只是简单地在引号内打印语句(如x=%2d ),如 print function 它会按原样打印“”中的任何内容。

But later I ran the code in my compiler to find the output as x=4199232 (The number varied in different compilers though) (Edit- I added the compiled code here)但后来我在我的编译器中运行代码以找到 output 为x=4199232 (虽然不同编译器中的数字有所不同)(编辑-我在这里添加了编译代码)

#include <stdio.h>

int main() {
    float x = 1.234;
    printf("x=%2d");
    return 0;
}

Can anybody kindly explain me what is really happening here.任何人都可以解释一下这里到底发生了什么。

The code has undefined behavior (which explains why the number varied in different compilers ) because you do not provide an argument of type int for the conversion %2d .该代码具有未定义的行为(这解释了为什么不同编译器中的数字不同),因为您没有为转换提供int类型的参数%2d

If you had written this:如果你写了这个:

x = 1.234;
printf("x=%2d", x);

The output would depend on the type of x which does not appear in the code fragment. output 将取决于代码片段中未出现的x类型。 If x is defined with type int or a smaller integer type, including _Bool , the output should be x= 1 , but if x has any other arithmetic type, including float and double , the behavior is again undefined because the argument does not have the expected type for %d .如果x使用int类型或更小的 integer 类型定义,包括_Bool ,则 output 应该是x= 1 ,但如果x具有任何其他算术类型,包括floatdouble ,则行为再次未定义,因为参数没有%d的预期类型。

Note also that there is no trailing \n in the format string, so the output might be delayed until the end of the program and might not appear at all on some non conformant systems.另请注意,格式字符串中没有尾随\n ,因此 output 可能会延迟到程序结束,并且可能根本不会出现在某些不符合要求的系统上。

In your sample code, the behavior is undefined because of the missing argument to printf , but you do define x as a float , which would be implicitly converted as a double when passed to printf , invalid for %d .在您的示例代码中,由于缺少printf的参数,因此行为未定义,但您确实将x定义为float ,当传递给printf时,它将被隐式转换为double ,对于%d无效。

Here is a modified version:这是修改后的版本:

#include <stdio.h>

int main() {
    double x = 1.234;            // only use `float` when necessary
    printf("x=%2d\n", (int)x);   // outputs `x= 1`
    printf("x=%2f\n", x);        // outputs `x=1.234000`
    printf("x=%2g\n", x);        // outputs `x=1.234`
    printf("x=%.2f\n", x);       // outputs `x=1.23`
    return 0;
}

In this statement在这份声明中

printf("x=%2d");

you forgot to specify an argument for the conversion specifier d .您忘记为转换说明符d指定参数。 So the program will try to output whatever is stored in the memory where the second argument should be.所以程序将尝试 output 存储在 memory 中的第二个参数应该是什么。

So the program has undefined behavior.所以程序有未定义的行为。

It will also have undefined behavior if you will specify the second argument like如果您指定第二个参数,它也会有未定义的行为

printf("x=%2d", x );

because there is used an invalid conversion specifier with an object of the type float.因为使用了无效的转换说明符和浮点类型的 object。

To output just the format string you should write到 output 只是你应该写的格式字符串

printf("x=%%2d");

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

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