function returning __int32 in place of __int64

H

hurry

hi,

I am writing a c program in VC++ 6.

I have 2 files with 3 functions.

file-1 having two functions "a" and "c"
file-2 having a single function "b"

with function "a" as main() , "a" calls function "b" in file-2 which,
in turn calling function "c" in file-1.

function "c" and "b" are declared to return __int64. function "c"
returns it correctly but when it is assigned, it becomes a __int32 bit
number ( assigned to a __int64 var).
and interestingly, when i copy and paste the function "c" from file-2
to file 1, the program behaves as expected.

the code as is given below.

when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216


file-1:

#include<stdio.h>
__int64 a();
__int64 b();

void main()
{
__int64 x;
x = b();
}

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

file-2:

__int64 b()
{
__int64 z;
z = c();
return z;
}
 
T

tmp123

Hi,

Function "c" has no prototype in file-2.
Thus, the compiler (VC in this case) will asume "int".

Sugestion. Add "__int64 b(void);" at start of file-2.

Kind regards.
 
A

Anand

hurry said:
hi,

I am writing a c program in VC++ 6. [...]

when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216
[...]
__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}
[...]
__int64, and __int32 might be windows/VC specific data types and not std
C. So I am assuming all it is trying to represent is integer types of 64
bit and 32 bit long.

From what's provided in the post, the problem might be in the
statement: y = 9007199237963776;
The answer lies in sizeof(int) on your system(and compiler, of course.)

9007199237963776 is an integer constant of type 'int'.
1FFFFFFF000000 which is more than 32 bits and so this might be getting
truncated even before getting assigned to y.

You can try doing, instead,
y = 9007199237963776L; or
y = 9007199237963776LL;
 
A

Anand

Anand said:
hurry said:
hi,

I am writing a c program in VC++ 6.
[...]


when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216
[...]

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

[...]
__int64, and __int32 might be windows/VC specific data types and not std
C. So I am assuming all it is trying to represent is integer types of 64
bit and 32 bit long.

From what's provided in the post, the problem might be in the
statement: y = 9007199237963776;
The answer lies in sizeof(int) on your system(and compiler, of course.)

9007199237963776 is an integer constant of type 'int'.
1FFFFFFF000000 which is more than 32 bits and so this might be getting
truncated even before getting assigned to y.

You can try doing, instead,
y = 9007199237963776L; or
y = 9007199237963776LL;
And, of course as tmp123 has rightly pointed out, the function c's
missing prototype might also be a strong (and main) candidate for the
problem.
 
F

Flash Gordon

Anand said:
Anand said:
hurry said:
hi,

I am writing a c program in VC++ 6.
[...]


when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216
[...]

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

[...]
__int64, and __int32 might be windows/VC specific data types and not
std C. So I am assuming all it is trying to represent is integer types
of 64 bit and 32 bit long.

From what's provided in the post, the problem might be in the
statement: y = 9007199237963776;
The answer lies in sizeof(int) on your system(and compiler, of course.)

9007199237963776 is an integer constant of type 'int'.
1FFFFFFF000000 which is more than 32 bits and so this might be getting
truncated even before getting assigned to y.

I think you need to learn a bit more about C since you are wrong. From
the latest standard:
| The type of an integer constant is the first of the corresponding list
| in which its value can be represented.
| Suffix DecimalConstant Constant
| none int int
| long int unsigned int
| long long int long int
| unsigned long int
| long long int
| unsigned long long int
....
| If an integer constant cannot be represented by any type in its list,
| it may have an extended integer type, if the extended integer type can
| represent its value. If all of the types in the list for the constant
| are signed, the extended integer type shall be signed. If all of the
| types in the list for the constant are unsigned, the extended integer
| type shall be unsigned. If the list contains both signed and unsigned
| types, the extended integer type may be signed or unsigned.

So basically it will an int if it fits, otherwise it will be the
smallest type in which it fits.
And, of course as tmp123 has rightly pointed out, the function c's
missing prototype might also be a strong (and main) candidate for the
problem.

That will definitely be the problem, since without at least a
declaration in scope the compiler is required to assume the function
returns an int which of course can produce very strange results if that
is not true.

2 morals to this:
1) Don't give advice when you don't know the answer
2) Always use headers to provide the correct prototypes for functions.
 
J

Jordan Abel

y = 9007199237963776L; or

Not likely to fix it if long is 32 bits [AIUI likely on windows]
y = 9007199237963776LL;

Not likely if the compiler doesn't support "long long"

besides - IIRC integer literals have to be of a type large enough to
contain them if such a type exists.
 
N

Niklas Norrthon

hurry said:
hi,

I am writing a c program in VC++ 6.

I have 2 files with 3 functions.

file-1 having two functions "a" and "c"
file-2 having a single function "b"

with function "a" as main() , "a" calls function "b" in file-2 which,
in turn calling function "c" in file-1.

function "c" and "b" are declared to return __int64. function "c"
returns it correctly but when it is assigned, it becomes a __int32 bit
number ( assigned to a __int64 var).
and interestingly, when i copy and paste the function "c" from file-2
to file 1, the program behaves as expected.

the code as is given below.

when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216


file-1:

#include<stdio.h>
__int64 a();
__int64 b();

No such types in C.

typedef long long int64;
/* or if you really insist :
typedef __some_implementation_specific_type int64;
*/
void main()

int main(void)

The return type of main must be int! And the argument list
should be either (void), or (int, char**).
{
__int64 x;
x = b();
}

Ever heard of indentation?

{
int64 x;
x = b(); /* or int64 x = b(); */
__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

More indentation, and a little bit of simplification:

int64 c()
{
return 9007199237963776;
}

Function below calls c() without prototype in scope.

int64 c();

In the current standard a diagnostic is required. In the previous standard
return type of int is assumed, but I'd be surprised if you don't get a
warning if you use proper flags to the compiler (if I remember correctly it
is /W4 on microsoft's compilers).
__int64 b()
{
__int64 z;
z = c();
return z;
}

int64 b()
{
return c();
}

/Niklas Norrthon
 
H

haroon

hurry said:
hi,

I am writing a c program in VC++ 6.

I have 2 files with 3 functions.

file-1 having two functions "a" and "c"
file-2 having a single function "b"
[...]

when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216
if you compiled the program, you might have noticed an error saying

"c undefined assuming extern returning int"

now read 6.8.6.4 bullet number 3 of C99 draft.

this explains alot, doesn't it?
 
H

haroon

haroon wrote:

[...]
if you compiled the program, you might have noticed an error saying
pardon me, it's a warning not an error.

P.S. hurry, you if u put some thing like `extern __int64 c();'
on top of file-2 (where you defined c) it seems to work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top