gpt4 book ai didi

c - 打印到标准输出时 printf 产生额外的 %

转载 作者:行者123 更新时间:2023-12-03 11:56:53 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>

int main() {
int year = 2017;
int month = 8;
int date = 8;

printf("%i/%i/%i", date, month, year);
return EXIT_SUCCESS;
}

这是我的代码。我想打印

8/8/2017

但它实际上打印了

8/8/2017%

我该如何解决这个问题?

最佳答案

你在使用 zsh 吗?当您的程序打印没有尾随换行符的输出时,某些 shell 会打印 %% 是一个标准定界符,用于将程序输出与下一个 shell 提示符分开。看Why ZSH ends a line with a highlighted percent symbol?

在打印语句的末尾添加一个换行符以摆脱它:

printf("%i/%i/%i\n",date , month , year);

我的 Macintosh 上有 zsh,我可以重现这个行为。这是一个简单的 C 程序:

#include <stdio.h>

int main() {
printf("Hello world!");

return 0;
}

Output :

$ ./a.out
Hello world!%

关于c - 打印到标准输出时 printf 产生额外的 %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558604/

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