如何给printf很长很长 [英] How to printf long long

查看:416
本文介绍了如何给printf很长很长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了aproximate PI的程序,我想长长使用,但它不工作。
这里是code

 #包括LT&;&stdio.h中GT;
#包括LT&;&MATH.H GT;
typedef的长长NUM;
主要(){
    NUM PI;
    圆周率= 0;
    NUM E,N;
    scanf函数(%d个,&安培; N);
    对于(E = 0; 1,E ++){
      PI + =((POW(( - 1.0),E))/(2.0 * E + 1.0));
      如果(E%N == 0)
        的printf(%15lld - >%1.16lld \\ N,即4 * PI);
      //的printf(%LLD \\ n,4 * PI);
    }
}


解决方案

显然%LLD 是最常用的方式,但不会对编译器的工作,我M使用(的mingw32-GCC v4.6.0)。做这个编译器的方法是:%I64d号

所以,试试这个:

 如果(E%N == 0)printf的(%15I64d  - >%1.16I64d \\ N,即4 * PI);

  scanf函数(%I64d号,&安培; N);

据我所知,在一个完全可移植的方式这样做的唯一方法是使用定义< inttypes.h方式>

在您的情况下,它应该是这样的:

  scanf函数(%SCNd64,&安培; N);
// ...
如果(E%N == 0)printf的(%15PRId64 - >%1.16PRId64\\ n,即4 * PI);

这真的是非常难看......但至少它是可移植。

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

#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);
    }
}

解决方案

Apparently %lld is the most common 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

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>.

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.

这篇关于如何给printf很长很长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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