开发者

python to C conversion error

python code

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

c code

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

I am getting an error saying: error: integer constant is too large for "long" type at both printf statement in c code. 'myfunc' function returns a long. This can be solved by defining 'b' a different type. I tried defining 'b' as 'long' and 'unsigned long' but no help. Any pointers?


My bad...This is short version of problem

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

I am getting error and warnings: error: integer constant is too large for "long" type warning: conversion lacks type at end of format war开发者_运维问答ning: too many arguments for format


Your C code needs braces to create the scope that Python does by indentation, so it should look like this:

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


Try long long. Python automatically uses number representation which fits your constants, but C does not. 0x100000000L simply does not fit in 32-bit unsigned int, unsigned long and so on. Also, read your C textbook on long long data type and working with it.


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" */
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜