【问题标题】:python to C conversion errorpython到C转换错误
【发布时间】:2011-04-17 17:34:24
【问题描述】:

python 代码

for b in range(4):
    for c in range(4):
        print myfunc(b/0x100000000, c*8)

c 代码

unsigned int b,c;
for(b=0;b<4;b++)
    for(c=0;c<4; c++)
    printf("%L\n", b/0x100000000);
    printf("%L\n" , myfunc(b/0x100000000, c*8)); 

我收到一条错误消息: 错误:整数常量对于 c 代码中的两个 printf 语句中的“long”类型来说太大。 'myfunc' 函数返回一个 long。 这可以通过将“b”定义为不同的类型来解决。我尝试将'b'定义为'long'和'unsigned long'但没有帮助。 有什么指点吗?


我的错...这是问题的简短版本

unsigned int b;
b = 1;
printf("%L", b/0x100000000L);

我收到错误和警告: 错误:整数常量对于“long”类型来说太大了 警告:转换在格式末尾缺少类型 警告:格式参数过多

【问题讨论】:

  • 您的 C 代码中缺少一些花括号。
  • 对于长整型常量使用0x100000000L
  • 您期望这里的输出是什么?任何整数类型除以更大的值都将为零。

标签: python c


【解决方案1】:

您的 C 代码需要大括号来创建 Python 通过缩进执行的范围,因此它应该如下所示:

unsigned int b,c;
for(b=0;b<4;b++)
{
    for(c=0;c<4; c++)
    {
      printf("%L\n", b/0x100000000);
      printf("%L\n" , myfunc(b/0x100000000, c*8)); 
    }
}

【讨论】:

    【解决方案2】:

    试试long long。 Python 自动使用适合您的常量的数字表示,但 C 没有。 0x100000000L 根本不适合 32 位 unsigned intunsigned long 等等。另外,请阅读有关 long long 数据类型的 C 教科书并使用它。

    【讨论】:

      【解决方案3】:
      unsigned int b,c;
      const unsigned long d = 0x100000000L; /* 33 bits may be too big for int */
      
      for(b=0;b<4;b++) {
          for(c=0;c<4; c++) { /* use braces and indent consistently */
              printf("%ud\n", b/d); /* "ud" to print Unsigned int in Decimal */
              printf("%ld\n", myfunc(b/d, c*8));  /* "l" is another modifier for "d" */
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2011-12-27
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多