长期划分c

时间:2015-09-21 09:30:00

标签: c

我试图传递一个长长的"数字,问题是当我试图将这个数字除以10时,答案是不正确的..

#include <stdio.h>
int main(void)
{
    int n = 0 ;
    long long s = 4111111111111111;

    n = s % 10 ;
    printf("n after modulos %i\n",n );

    s = s / 10 ;
    printf("this is s after division %llo \n",s  );


    return 0;
}

输出:

n after modulos 1
this is s after division 13536350357330707 

3 个答案:

答案 0 :(得分:1)

printf("this is s after division %llo \n",s  );
                                   ^ (this prints (correct)value in octal representation)

使用说明符%lld(获取十进制值)。

答案 1 :(得分:1)

  

man 3 printf

   o, u, x, X
          The  unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation.  The letters abcdef are used for x conversions; the letters ABCDEF are
          used for X conversions.  The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with  zeros.   The  default
          precision is 1.  When 0 is printed with an explicit precision 0, the output is empty.

还有一些关于手册的内容

  

man 3p printf

   ll (ell-ell)

          Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long or unsigned long long argument; or that a following n conversion specifier applies to a pointer  to  a  long
          long argument.

答案 2 :(得分:0)

要打印long long,您应该使用%lld而不是%llo

%llo格式用于表示long long中的octal值。