如何通过printf的打印正数为负数 [英] How to print a positive number as a negative number via printf

查看:687
本文介绍了如何通过printf的打印正数为负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读有关printf()的,我发现,根据需要由用户通过以下code(为 - )可以打印编号为阳性或阴性,但code不工作,输出是一个积极value.Please提到那里的错误is.Thanks

 #包括LT&;&stdio.h中GT;
诠释的main()
{
  的printf(%-d \\ n,1977年);
  返回0;
}


解决方案

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

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

 的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

这篇关于如何通过printf的打印正数为负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆