C/C++ 输出整数带正负号

格式控制符设为 %+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
发布了105 篇原创文章 · 获赞 24 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/Kwansy/article/details/103620004