gpt4 book ai didi

c - 如何在 C 中操作一般输出(不是数字或变量)?

转载 作者:行者123 更新时间:2023-11-30 15:21:38 26 4
gpt4 key购买 nike

好吧,我正在用 C 语言做这个作业,我想知道 C 语言是否有像 c++ 中的 setw 这样的输出操纵器?我知道如何操纵数字和其他东西,但我不知道如何操纵一般输出。例如

我想输出宽度为5的“process”和“count”:

printf("Process") 和 printf("count"),它们之间的宽度为 5。我怎样才能做到这一点?

最佳答案

我不确定 setw(n) 是否像您想象的那样工作。如果你编译并运行这个C++代码。 。 .

#include <sstream>
#include <iostream>
#include <iomanip>

int main()
{
std::cout << "Process"
<< std::setw(5) << "count" << '\n';
}

然后你会得到这个输出。

Processcount

Use setw(10) instead, and you'll get this output.

Process     count

In C, printf() lets you specify the column width.

/* File: code/c/test.c */
#include <stdio.h>

int main(void) {
printf("%s", "Process");
printf("%10s\n", "count");

return 0;
}

这将首先打印“Process”,然后在 10 个字符宽的字段中打印“count”。字符串“count”本身有五个字符。这会在两个字符串之间留下五个空格。

$ gcc -Wall code/c/test.c
$ ./a.out
Process     count

嵌入像这样的制表符 printf("Process\tcount\n"); 不会在两个单词之间留下五个空格。

Process count

关于c - 如何在 C 中操作一般输出(不是数字或变量)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com