c - C:尝试使用 \ 将 fprintf() 格式字符串拆分为多行并在行的开头添加制表符
问题描述
我正在尝试使用如下所示的字符将 long format string
to拆分fprintf()
为多行:\
fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\
And returns the day of the week for that date using Zeller's rule.\n\n\
Enter date in (dd/mm/yyyy) format: ");
这会导致whitespaces
添加到输出中,如下所示:
This program take a date supplied by the user in dd/mm/yyyy format...
And returns the day of the week for that date using Zeller's rule.
Enter date in (dd/mm/yyyy) format:
这个答案表明这应该有效。在发布到这里之前,我还检查了这个答案。对它的评论提到这种方法......
...遭受这样一个事实,即如果在“\”之后有任何空格,它就会中断;发生时可能会令人困惑的错误。
cat -A
程序文件中的输出...
^Ifprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\$
^I^I^IAnd returns the day of the week for that date using Zeller's rule.\n\n\$
^I^I^IEnter date in (dd/mm/yyyy) format: ");$
\
...在;之后不显示空格 尽管它确实将<TAB>
s 引入到后面的行中。我vim
用来编辑我的源文件。
\
我在 中一直使用行继续Bash
,并认为它与fprintf()
C 中的格式字符串类似。
我想保持我的代码可读性和合理的线宽。除了将长字符串拆分为多个fprintf()
.
- 这是标准行为吗
printf()/fprintf()
vim
当我用 继续行时,我的代码是否正常\
?- 我该如何解决这个问题?
解决方案
您不需要\
,因为它不是宏定义。
只需将任意数量的字符串文字用任意数量的空格分隔即可(新行也是空格)。C 编译器会忽略空格。
int main(void)
{
fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n"
"And returns the day of the week for that date using Zeller's rule.\n\n"
"Enter date in (dd/mm/yyyy) format: ");
}
int main(void)
{
fprintf(stdout, "This program take a date"
" supplied by the user in dd/"
"mm/yyyy format...\n"
"And returns "
"the "
"day of "
"the "
"week for that "
"date using Zeller's rule.\n\n"
"Enter date in (dd/mm/yyyy) format: ");
}
推荐阅读
- ruby - 来自 ruby 服务器的 fork 进程
- flutter - 插件 `XXXXX` 使用了已弃用的 Android 嵌入版本
- next.js - 如何为下一个js图像添加边框半径?
- javascript - 如果我单击反应 js 中的关闭按钮,如何更改状态
- xamarin - 如何在 Xamarin 中获取绑定之外的数据
- java - 无法从 Spring Boot 应用程序和 SQL Developer 访问 Docker 上的 DB2 数据库
- php - 如果在 laravel 自定义验证中包含多个值,则不会验证输入数组
- python - Python:如果任何行项目为空,熊猫将丢弃所有行
- python-3.x - 在 python 中处理 USR1 信号
- css - 如何在关键帧动画后隐藏元素