C语言 如何打印长长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6400180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:57:01  来源:igfitidea点击:

How to printf long long

clong-integerpi

提问by Carlos

I'm doing a program that aproximate PI and i'm trying to use long long, but it isn't working. Here is the code

我正在做一个近似 PI 的程序,我正在尝试使用 long long,但它不起作用。这是代码

#include<stdio.h>
#include<math.h>
typedef long long num;
main(){
    num pi;
    pi=0;
    num e, n;
    scanf("%d", &n);
    for(e=0; 1;e++){
      pi += ((pow((-1.0),e))/(2.0*e+1.0));
      if(e%n==0)
        printf("%15lld -> %1.16lld\n",e, 4*pi);
      //printf("%lld\n",4*pi);
    }
}

回答by karadoc

%lldis the standard C99 way, but that doesn't work on the compiler that I'm using (mingw32-gcc v4.6.0). The way to do it on this compiler is: %I64d

%lld是标准的 C99 方式,但这不适用于我使用的编译器(mingw32-gcc v4.6.0)。在此编译器上执行此操作的方法是:%I64d

So try this:

所以试试这个:

if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi);

and

scanf("%I64d", &n);

The only way I know of for doing this in a completely portable way is to use the defines in <inttypes.h>.

我所知道的以完全可移植的方式执行此操作的唯一方法是使用<inttypes.h>.

In your case, it would look like this:

在你的情况下,它看起来像这样:

scanf("%"SCNd64"", &n);
//...    
if(e%n==0)printf("%15"PRId64" -> %1.16"PRId64"\n",e, 4*pi);

It really is very ugly... but at least it is portable.

它真的很丑……但至少它是便携的。

回答by Jonathan Leffler

  • Your scanf()statement needs to use %lldtoo.
  • Your loop does not have a terminating condition.
  • There are far too many parentheses and far too few spaces in the expression

    pi += pow(-1.0, e) / (2.0*e + 1.0);
    
  • You add one on the first iteration of the loop, and thereafter zero to the value of 'pi'; this does not change the value much.
  • You should use an explicit return type of intfor main().
  • On the whole, it is best to specify int main(void)when it ignores its arguments, though that is less of a categorical statement than the rest.
  • I dislike the explicit licence granted in C99 to omit the return from the end of main()and don't use it myself; I write return 0;to be explicit.
  • 您的scanf()语句也需要使用%lld
  • 您的循环没有终止条件。
  • 表达式中有太多的括号和太少的空格

    pi += pow(-1.0, e) / (2.0*e + 1.0);
    
  • 您在循环的第一次迭代中添加一个,然后将 'pi' 的值设为零;这不会改变太多的价值。
  • 您应该使用显式返回类型intfor main()
  • 总的来说,最好指定int main(void)它何时忽略其参数,尽管与其他语句相比,这不是一个明确的声明。
  • 我不喜欢 C99 中授予的显式许可,以省略末尾的返回main()并且不自己使用它;我写return 0;得很明确。

I think the whole algorithm is dubious when written using long long; the data type probably should be more like long double(with %Lffor the scanf()format, and maybe %19.16Lffor the printf()formats.

我认为整个算法在使用long long;编写时是可疑的;数据类型可能应该更像long double%Lf用于scanf()格式,也可能%19.16Lf用于printf()格式。

回答by Atul Kumar

    // acos(0.0) will return value of pi/2, inverse of cos(0) is pi/2 
    double pi = 2 * acos(0.0);
    int n; // upto 6 digit
    scanf("%d",&n); //precision with which you want the value of pi
    printf("%.*lf\n",n,pi); // * will get replaced by n which is the required precision

回答by Vinicius Kamakura

First of all, %d is for a int

首先, %d 是一个 int

So %1.16lldmakes no sense, because %d is an integer

所以%1.16lld没有意义,因为 %d 是一个整数

That typedef you do, is also unnecessary, use the type straight ahead, makes a much more readable code.

你做的 typedef 也是不必要的,直接使用类型,使代码更具可读性。

What you want to use is the type double, for calculating pi and then using %for %1.16f.

您要使用的是类型double,用于计算 pi 然后使用%for %1.16f