格式控制符设为 %+d 即可
#include <bits/stdc++.h>
using namespace std;
int main() {
printf("%+d\n", -1);
printf("%+d\n", 0);
printf("%+d\n", 1);
cout << showpos << -1 << endl; // noshowpos 可以取消显示正号
cout << showpos << 0 << endl;
cout << showpos << 1 << endl;
return 0;
}
输出:
-1
+0
+1
-1
+0
+1
本文详细介绍了在C++中使用格式控制符%+d的方法,展示了如何在printf和cout中显示带符号的整数,包括正数、负数和零的正确输出格式。
6973

被折叠的 条评论
为什么被折叠?



