且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过printf的打印正数为负数

更新时间:2022-10-18 13:09:26

从您的意见看起来你看错此页面。在 - + 符做两件完全不同的事情,而且也不是什么你认为' - '应该做的

正如其他人所指出的那样, - 不离开的理由。在 + 说明打印正数与一个领先的加号(数和负数仍然获得领先的减号):

 的printf(%D%+ D%+ D \\ n,10,10,10,10);

输出:


  

-10 10 -10 +10


块引用>

While reading about printf(),I found that it can print numbers as positive or negative as desired by the user by following code(for -).But the code doesnt work and the output is a positive value.Please mention where the error is.Thanks

#include<stdio.h>
int main()
{
  printf (" %-d\n", 1977);
  return 0;
}

From your comments it looks like you misread this page. The - and + specifiers do two completely different things, and neither does what you think '-' should do.

As others have noted, - does left justification. The + specifier prints positive numbers with a leading plus sign (and negative numbers still get a leading minus sign):

printf("%d %d %+d %+d\n", -10, 10, -10, 10);

outputs:

-10 10 -10 +10