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

格式控制符设为 %+d 即可

#include 
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/C++ 输出整数带正负号)